-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPool.h
More file actions
58 lines (42 loc) · 1.05 KB
/
ConnectionPool.h
File metadata and controls
58 lines (42 loc) · 1.05 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
49
50
51
52
53
54
55
56
57
58
#ifndef HTTPSERVER_CONNECTIONPOOL_H
#define HTTPSERVER_CONNECTIONPOOL_H
#include "Mutex.h"
#include "ConditionVar.h"
#include <mysql.h>
#include <mysql.h>
#include <string>
#include <vector>
/**
* 单例模式连接池
*
*/
class ConnectionPool {
public:
ConnectionPool() = default;
~ConnectionPool();
void init(std::string url, std::string userName, std::string passWd,
std::string databaseName, int port, int maxConnNum);
static ConnectionPool* getInstance();
MYSQL* getConn();
void releaseConn(MYSQL* conn);
void destoryPool();
private:
std::string url_;
std::string userName_;
std::string passWd_;
std::string databaseName_;
int port_;
int maxConnNum_; // 最大连接数量
std::vector<MYSQL*> conns_;
Mutex mtx_;
ConditionVar cond_;
};
class ConnectionPoolRAII {
public:
ConnectionPoolRAII(MYSQL** mysql, ConnectionPool* connPool);
~ConnectionPoolRAII();
private:
MYSQL* mysql_;
ConnectionPool* connPool_;
};
#endif // HTTPSERVER_CONNECTIONPOOL_H