-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmySQLConnectionPool.h
More file actions
48 lines (41 loc) · 1.1 KB
/
mySQLConnectionPool.h
File metadata and controls
48 lines (41 loc) · 1.1 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
45
46
47
48
#ifndef MYSQL_CONNECTION_POOL_H
#define MYSQL_CONNECTION_POOL_H
#include <iostream>
#include <chrono>
#include <string>
#include <vector>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/resultset.h>
#include <boost/variant.hpp>
using namespace std;
using namespace sql;
class MySQLConnectionPool {
public:
MySQLConnectionPool(const string& host, const string& user, const string& password, const string& database, int poolSize, int heartbeatInterval);
~MySQLConnectionPool();
Connection* getConnection();
void releaseConnection(Connection* conn);
private:
void initializePool();
void startHeartbeat();
void stopHeartbeat();
void checkConnections();
private:
string host_;
string user_;
string password_;
string database_;
int poolSize_;
int heartbeatInterval_;
sql::mysql::MySQL_Driver* driver_;
vector<sql::Connection*> connectionPool_;
mutex mutex_;
condition_variable condition_;
thread heartbeatThread_;
bool heartbeatRunning_ = true;
};
#endif