-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPool.cpp
More file actions
44 lines (37 loc) · 1.57 KB
/
ConnectionPool.cpp
File metadata and controls
44 lines (37 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "ConnectionPool.h"
/*
The purpose of a connection pool is to
optimise the management and use of connections to the database (DB) in the thread pool server.
*/
// Constructor to initialize the connection pool with the connection information and pool size
ConnectionPool::ConnectionPool(const std::string& conninfo, size_t poolSize)
: conninfo_(conninfo), poolSize_(poolSize) {
for (size_t i = 0; i < poolSize_; ++i) {
pool_.emplace(std::make_shared<pqxx::connection>(conninfo_));
}
}
// Destructor to clear the connection pool
ConnectionPool::~ConnectionPool() {
// Lock the mutex to access the pool, ensuring that no other thread is using the pool
std::lock_guard<std::mutex> lock(mutex_);
while (!pool_.empty()) {
pool_.pop();
}
}
std::shared_ptr<pqxx::connection> ConnectionPool::getConnection() {
// Lock the mutex to access the pool, ensuring that no other thread is using the pool,
// avoid the case of multiple threads accessing the pool at the same time and then they get the same connection
std::unique_lock<std::mutex> lock(mutex_);
condition_.wait(lock, [this]() { return !pool_.empty(); });
// Get the connection from the front of the pool
auto conn = pool_.front();
pool_.pop();
return conn;
}
// Release the connection back to the pool
void ConnectionPool::releaseConnection(std::shared_ptr<pqxx::connection> conn) {
std::lock_guard<std::mutex> lock(mutex_);
pool_.push(conn);
// Notify one of the waiting threads that there is a connection available
condition_.notify_one();
}