Skip to content

Commit 3e54141

Browse files
committed
improve databasehandler
1 parent fede096 commit 3e54141

File tree

6 files changed

+61
-63
lines changed

6 files changed

+61
-63
lines changed

src/controllers/databasecontroller/databasecontroller.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include <unordered_set>
1313
#include <utility>
1414

15-
#include "database/connectionhandler.hpp"
1615
#include "database/databaseconnectionpool.hpp"
16+
#include "database/databasehandler.hpp"
1717
#include "utils/global/types.hpp"
1818
#include "utils/message/message.hpp"
1919
class Case;
@@ -49,17 +49,23 @@ class DatabaseController
4949
template <typename Result, typename Func, typename... Args>
5050
std::optional<Result> executer(const Func &func, Args &&...args)
5151
{
52-
std::optional<Result> results;
53-
ConnectionHanndler con_handler(databaseConnectionPool);
54-
5552
try
5653
{
57-
if (con_handler.get_connection() == nullptr)
54+
std::unique_ptr<DatabaseHanndler> connectionHanndler = std::make_unique<DatabaseHanndler>();
55+
56+
if (connectionHanndler->get_connection() == nullptr)
5857
{
5958
return std::nullopt;
6059
}
6160

62-
results = std::invoke(func, con_handler.get_connection(), std::forward<Args>(args)...);
61+
std::optional<Result> results = std::invoke(func, connectionHanndler->get_connection().get(), std::forward<Args>(args)...);
62+
63+
if (results.has_value())
64+
{
65+
return results;
66+
}
67+
68+
return std::nullopt;
6369
}
6470
catch (const std::exception &e)
6571
{
@@ -71,11 +77,5 @@ class DatabaseController
7177
Message::CriticalMessage("Unknown exception occurred during query execution.");
7278
return std::nullopt;
7379
}
74-
75-
if (results.has_value())
76-
{
77-
return results;
78-
}
79-
return std::nullopt;
8080
}
8181
};

src/database/connectionhandler.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/database/connectionhandler.hpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/database/databasehandler.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "database/databasehandler.hpp"
2+
3+
#include <memory>
4+
#include <utility>
5+
6+
#include "database/databaseconnectionpool.hpp"
7+
#include "store/store.hpp"
8+
9+
DatabaseHanndler::DatabaseHanndler() : databaseConnectionPool(Store::getObject<DatabaseConnectionPool>()) { db_ptr = databaseConnectionPool->get_connection(); }
10+
11+
DatabaseHanndler::~DatabaseHanndler()
12+
{
13+
if (db_ptr != nullptr)
14+
{
15+
databaseConnectionPool->return_connection(std::move(db_ptr));
16+
}
17+
}
18+
19+
std::shared_ptr<Database> DatabaseHanndler::get_connection() { return db_ptr; }

src/database/databasehandler.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <memory>
4+
5+
#include "database/databaseconnectionpool.hpp"
6+
class DatabaseHanndler
7+
{
8+
public:
9+
DatabaseHanndler();
10+
DatabaseHanndler(const DatabaseHanndler &) = default;
11+
DatabaseHanndler(DatabaseHanndler &&) = delete;
12+
DatabaseHanndler &operator=(const DatabaseHanndler &) = default;
13+
DatabaseHanndler &operator=(DatabaseHanndler &&) = delete;
14+
virtual ~DatabaseHanndler();
15+
16+
std::shared_ptr<Database> get_connection();
17+
18+
private:
19+
std::shared_ptr<DatabaseConnectionPool> databaseConnectionPool;
20+
std::shared_ptr<Database> db_ptr;
21+
};

src/database/watchdog.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
#include <memory>
88
#include <stdexcept>
99
#include <thread>
10-
#include <utility>
1110

1211
#include "database/database.hpp"
1312
#include "database/databaseconnectionpool.hpp"
13+
#include "database/databasehandler.hpp"
1414
#include "store/store.hpp"
1515
#include "utils/message/message.hpp"
1616

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

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

41-
if (!db_ptr->check_connection())
41+
if (!connectionHanndler->get_connection()->check_connection())
4242
{
4343
Message::WarningMessage("Database connection lost. Attempting to reconnect...");
44-
Message::CriticalMessage(fmt::format("Database connection {} link is lost.", static_cast<const void *>(db_ptr.get())));
44+
Message::CriticalMessage(
45+
fmt::format("Database connection {} link is lost.", static_cast<const void *>(connectionHanndler->get_connection().get())));
4546
try
4647
{
47-
if (db_ptr->reconnect())
48+
if (connectionHanndler->get_connection()->reconnect())
4849
{
49-
Message::InfoMessage(fmt::format("Database connection id: {} link is re-established", static_cast<void *>(db_ptr.get())));
50+
Message::InfoMessage(fmt::format(
51+
"Database connection id: {} link is re-established", static_cast<void *>(connectionHanndler->get_connection().get())));
5052
databaseConnectionPool->reconnect_all();
5153
}
5254
else
@@ -60,7 +62,6 @@ WatchDog::WatchDog() : databaseConnectionPool(Store::getObject<DatabaseConnectio
6062
}
6163
}
6264

63-
databaseConnectionPool->return_connection(std::move(db_ptr));
6465
std::this_thread::sleep_for(check_interval);
6566
}
6667
catch (const std::exception &e)

0 commit comments

Comments
 (0)