Skip to content

Commit

Permalink
fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
dr3mro committed Jan 1, 2025
1 parent 07c74c1 commit 3b9e998
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/controllers/databasecontroller/databasecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include <unordered_set>

#include "database/database.hpp"
#include "database/databaseconnectionpool.hpp"
#include "database/watchdog.hpp"
#include "store/store.hpp"
#include "utils/global/types.hpp"

DatabaseController::DatabaseController() : watchDog(Store::getObject<WatchDog>()) {}

DatabaseController::DatabaseController() : databaseConnectionPool_(Store::getObject<DatabaseConnectionPool>()), watchDog_(Store::getObject<WatchDog>()) {}
std::optional<jsoncons::json> DatabaseController::executeQuery(const std::string &query)
{
return executer<jsoncons::json>(&Database::executeQuery<jsoncons::json, pqxx::work>, query);
Expand Down
5 changes: 2 additions & 3 deletions src/controllers/databasecontroller/databasecontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
class Case;
class Service;
class Appointment;

class Database;
class WatchDog;

class DatabaseController
{
public:
Expand All @@ -44,7 +42,8 @@ class DatabaseController
std::optional<jsoncons::json> getPermissions(const std::string &query);

private:
std::shared_ptr<WatchDog> watchDog;
std::shared_ptr<DatabaseConnectionPool> databaseConnectionPool_;
std::shared_ptr<WatchDog> watchDog_;

template <typename Result, typename Func, typename... Args>
std::optional<Result> executer(const Func &func, Args &&...args)
Expand Down
5 changes: 3 additions & 2 deletions src/database/databaseconnectionpool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class DatabaseConnectionPool
std::deque<std::shared_ptr<Database>> databaseConnections;
std::mutex mutex;
std::condition_variable cv;
static constexpr std::uint16_t TIMEOUT = 2;
static constexpr unsigned int MAX_RETRIES = 5;

static constexpr std::uint16_t TIMEOUT = 2;
static constexpr unsigned int MAX_RETRIES = 5;
};
4 changes: 3 additions & 1 deletion src/database/databasehandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include <memory>

#include "database/databaseconnectionpool.hpp"
class DatabaseConnectionPool;
class Database;

class DatabaseHanndler
{
public:
Expand Down

0 comments on commit 3b9e998

Please sign in to comment.