Skip to content

Commit

Permalink
improve databasehandler
Browse files Browse the repository at this point in the history
  • Loading branch information
dr3mro committed Jan 1, 2025
1 parent fede096 commit 3e54141
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 63 deletions.
24 changes: 12 additions & 12 deletions src/controllers/databasecontroller/databasecontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <unordered_set>
#include <utility>

#include "database/connectionhandler.hpp"
#include "database/databaseconnectionpool.hpp"
#include "database/databasehandler.hpp"
#include "utils/global/types.hpp"
#include "utils/message/message.hpp"
class Case;
Expand Down Expand Up @@ -49,17 +49,23 @@ class DatabaseController
template <typename Result, typename Func, typename... Args>
std::optional<Result> executer(const Func &func, Args &&...args)
{
std::optional<Result> results;
ConnectionHanndler con_handler(databaseConnectionPool);

try
{
if (con_handler.get_connection() == nullptr)
std::unique_ptr<DatabaseHanndler> connectionHanndler = std::make_unique<DatabaseHanndler>();

if (connectionHanndler->get_connection() == nullptr)
{
return std::nullopt;
}

results = std::invoke(func, con_handler.get_connection(), std::forward<Args>(args)...);
std::optional<Result> results = std::invoke(func, connectionHanndler->get_connection().get(), std::forward<Args>(args)...);

if (results.has_value())
{
return results;
}

return std::nullopt;
}
catch (const std::exception &e)
{
Expand All @@ -71,11 +77,5 @@ class DatabaseController
Message::CriticalMessage("Unknown exception occurred during query execution.");
return std::nullopt;
}

if (results.has_value())
{
return results;
}
return std::nullopt;
}
};
21 changes: 0 additions & 21 deletions src/database/connectionhandler.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions src/database/connectionhandler.hpp

This file was deleted.

19 changes: 19 additions & 0 deletions src/database/databasehandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "database/databasehandler.hpp"

#include <memory>
#include <utility>

#include "database/databaseconnectionpool.hpp"
#include "store/store.hpp"

DatabaseHanndler::DatabaseHanndler() : databaseConnectionPool(Store::getObject<DatabaseConnectionPool>()) { db_ptr = databaseConnectionPool->get_connection(); }

DatabaseHanndler::~DatabaseHanndler()
{
if (db_ptr != nullptr)
{
databaseConnectionPool->return_connection(std::move(db_ptr));
}
}

std::shared_ptr<Database> DatabaseHanndler::get_connection() { return db_ptr; }
21 changes: 21 additions & 0 deletions src/database/databasehandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <memory>

#include "database/databaseconnectionpool.hpp"
class DatabaseHanndler
{
public:
DatabaseHanndler();
DatabaseHanndler(const DatabaseHanndler &) = default;
DatabaseHanndler(DatabaseHanndler &&) = delete;
DatabaseHanndler &operator=(const DatabaseHanndler &) = default;
DatabaseHanndler &operator=(DatabaseHanndler &&) = delete;
virtual ~DatabaseHanndler();

std::shared_ptr<Database> get_connection();

private:
std::shared_ptr<DatabaseConnectionPool> databaseConnectionPool;
std::shared_ptr<Database> db_ptr;
};
17 changes: 9 additions & 8 deletions src/database/watchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <memory>
#include <stdexcept>
#include <thread>
#include <utility>

#include "database/database.hpp"
#include "database/databaseconnectionpool.hpp"
#include "database/databasehandler.hpp"
#include "store/store.hpp"
#include "utils/message/message.hpp"

Expand All @@ -29,24 +29,26 @@ WatchDog::WatchDog() : databaseConnectionPool(Store::getObject<DatabaseConnectio
{
try
{
std::shared_ptr<Database> db_ptr = databaseConnectionPool->get_connection();
std::unique_ptr<DatabaseHanndler> connectionHanndler = std::make_unique<DatabaseHanndler>();

if (db_ptr == nullptr)
if (connectionHanndler->get_connection() == nullptr)
{
Message::WarningMessage("WatchDog could not acquire a database connection within timeout.");
std::this_thread::sleep_for(check_interval);
continue;
}

if (!db_ptr->check_connection())
if (!connectionHanndler->get_connection()->check_connection())
{
Message::WarningMessage("Database connection lost. Attempting to reconnect...");
Message::CriticalMessage(fmt::format("Database connection {} link is lost.", static_cast<const void *>(db_ptr.get())));
Message::CriticalMessage(
fmt::format("Database connection {} link is lost.", static_cast<const void *>(connectionHanndler->get_connection().get())));
try
{
if (db_ptr->reconnect())
if (connectionHanndler->get_connection()->reconnect())
{
Message::InfoMessage(fmt::format("Database connection id: {} link is re-established", static_cast<void *>(db_ptr.get())));
Message::InfoMessage(fmt::format(
"Database connection id: {} link is re-established", static_cast<void *>(connectionHanndler->get_connection().get())));
databaseConnectionPool->reconnect_all();
}
else
Expand All @@ -60,7 +62,6 @@ WatchDog::WatchDog() : databaseConnectionPool(Store::getObject<DatabaseConnectio
}
}

databaseConnectionPool->return_connection(std::move(db_ptr));
std::this_thread::sleep_for(check_interval);
}
catch (const std::exception &e)
Expand Down

0 comments on commit 3e54141

Please sign in to comment.