-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection_pool.cpp
More file actions
42 lines (32 loc) · 1010 Bytes
/
connection_pool.cpp
File metadata and controls
42 lines (32 loc) · 1010 Bytes
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
#include "connection_pool.hpp"
#include <exception>
#include <iostream>
#include <mutex>
#include "connector_guard.hpp"
ConnectionPool ConnectionPool::singleton;
ConnectorGuard ConnectionPool::GetConnector() {
std::unique_lock lock(queue_mtx_);
cv_.wait(lock, [this]() -> bool { return !free_list_.empty(); });
uint connector_idx = free_list_.front();
free_list_.pop();
return ConnectorGuard{&connectors_[connector_idx]};
}
void ConnectionPool::FreeConnector(uint idx) {
std::unique_lock lock(queue_mtx_);
free_list_.push(idx);
}
ConnectionPool::ConnectionPool() {
if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv_) != SQL_SUCCESS) {
std::cerr << "Error allocating environment handle\n";
std::terminate();
}
SQLSetEnvAttr(henv_, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
}
ConnectionPool::~ConnectionPool() {
for (int i = 0; i < max_connections_; ++i) {
connectors_[i].Disconnect();
}
if (henv_) {
SQLFreeHandle(SQL_HANDLE_ENV, henv_);
}
}