Repository navigation
Expand file tree
/
Copy pathconnectpool.cpp
More file actions
183 lines (178 loc) · 5.2 KB
/
Copy pathconnectpool.cpp
File metadata and controls
183 lines (178 loc) · 5.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* =====================================================================================
*
* Filename: connectpool.cpp
*
* Description: this file is the defination of the connectpool
*
* Version: 1.0
* Created: 08/22/2014 02:47:57 AM
* Revision: none
* Compiler: gcc
*
* Author: Yu Daoming (mn), yudaoming@360.cn
* Company: Qihoo 360 LTD.
*
* =====================================================================================
*/
#include "connectpool.h"
using namespace mysql_interface;
ConnectPool* ConnectPool::connectPoolInstance= (ConnectPool*)0;
//testhandle needed by the mysql connection function;maxSize is the max
//connection no of the pool
ConnectPool::ConnectPool(const Testhandle& info, int maxSize):realSize(0)
{
static CleanPool cleanpool;
this->info = info;
this->maxSize = maxSize;
log.init(("./mysql_connectpool.log"));
pthread_mutex_init(&lock,NULL);
}
//check whether the pool have enough connections!
bool ConnectPool::checkPool()
{
pthread_mutex_lock(&lock);
bool status;
if(!connectorList.size()&&realSize==maxSize)
status = false;
else status = true;
pthread_mutex_unlock(&lock);
return status;
}
//close all the connection and delete the connectPool
ConnectPool::~ConnectPool()
{
connectPoolInstance ->destoryConnectPool();
connectPoolInstance = (ConnectPool*)0;
pthread_mutex_destroy(&lock);
}
//put the connection back to the Connectpool;
void ConnectPool::freeConnection(Connector* connector)
{
if(connector)
{
pthread_mutex_lock(&lock);
connectorList.push_back(connector);
pthread_mutex_unlock(&lock);
}else{
log.write_log(confmgr::LEVEL_WARNING,"__ConnectPool__:__freeConnection__:__line__:42");
//Throw(SqlException,"__ConnectPool__:__free__Connection__:__line__48");
}
}
//get the instansce for singleton
ConnectPool* ConnectPool::getInstance(const Testhandle& info, int maxSize)
{
if(maxSize<0||maxSize==0)
throw 0;
if(maxSize>50)
maxSize = 50;
if(connectPoolInstance==0)
connectPoolInstance = new ConnectPool(info,maxSize);
connectPoolInstance->initConnectPool(maxSize);
return connectPoolInstance;
}
//if there are no connection in the pool ,this will be called to create a new connection
Connector* ConnectPool::createConnector()
{
Connector* connector_ptr = new Connector(info);
try{
connector_ptr->connect();
}catch(...)
{
log.write_log(confmgr::LEVEL_ERROR,"__ConnectPool__:__createConnector__:__line__:68");
return (Connector*)0;;
}
return connector_ptr;
}
//initial the pool after get the pool;
void ConnectPool::initConnectPool(int connectorSize)
{
Connector* connector_ptr;
connectorSize /= 2;
pthread_mutex_lock(&lock);
for(int i=0;i<connectorSize;i++)
{
connector_ptr = createConnector();
if(connector_ptr)
{
connectorList.push_back(connector_ptr);
realSize++;
}
}
pthread_mutex_unlock(&lock);
}
//this method free all the connections in the pool;
void ConnectPool::destoryConnectPool()
{
std::list<Connector*>::iterator iter;
//Connector conn_tr;
pthread_mutex_lock(&lock);
for(iter=connectorList.begin();iter!=connectorList.end();iter++)
{
if(!destoryConnection((*iter)))
log.write_log(confmgr::LEVEL_WARNING,"__ConnectPool__:destoryConnectPool:__line__:107");
}
connectorList.clear();
realSize=0;
pthread_mutex_unlock(&lock);
}
//destory connection
bool ConnectPool:: destoryConnection(Connector* connector)
{
//connector->closeConnect();
delete connector;
return true;
}
//get the connector from the poor
Connector* ConnectPool::getConnector()
{
Connector* connPtr;
pthread_mutex_lock(&lock);
//connectorList have connections
if(connectorList.size()>0)
{
connPtr = connectorList.front();
connectorList.pop_front();
//try to fix this invalid connector;
if(connPtr==0)
{
connPtr = createConnector();
}else if(!connPtr->is_connect())
{
try{
connPtr->connect();
}catch(SqlException)
{
delete connPtr;
connPtr = createConnector();
}
}
//if the effort that take to fix this connector, change the realSize!
if(!connPtr)
{
realSize--;
log.write_log(confmgr::LEVEL_FATAL,"__ConnectPool__:__getConnector__::__LINE__:156:Get connector failed!");
}
pthread_mutex_unlock(&lock);
return connPtr;
}
//connector is empty!
if(realSize < maxSize)
{
connPtr = createConnector();
if(connPtr)
{
realSize++;
}else
{
log.write_log(confmgr::LEVEL_WARNING,"__ConnectPool__:getConnector:__line__:141");
connPtr = (Connector*)0;
}
}else
{
log.write_log(confmgr::LEVEL_WARNING,"__ConnectPool__:getConnector:__line__:139:The Connections of connection have Exhausted!");
connPtr = (Connector*)0;
}
pthread_mutex_unlock(&lock);
return connPtr;
}