-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBPool.cxx
More file actions
405 lines (351 loc) · 15.4 KB
/
DBPool.cxx
File metadata and controls
405 lines (351 loc) · 15.4 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "DBPool.h"
#include "DBPoolConfiguration.h"
#include "DebugUtil.h"
#include "DBConnectionFactory.h"
#include <time.h>
// initialize our singleton instance
DBConnectionPools* DBPool::s_pools = NULL;
Mutex DBPool::s_initializationMutex;
//-------------------------------------------------------------------
// Singleton accessor method - this insures we have an automatic way
// to initialize the class (it will be initialized when this method is
// first called
DBPool& DBPool::getInstance(const string& poolName) throw (DBException)
{
// use a mutex to make sure we don't have multiple threads trying to
// initialize simultaneously. Since we are only concerned about
// initialization and not returning this back to the user
// we are only blocking around the initialization piece so wrap
// that in {} - this is done to try and make this as efficient as
// possible
{
MutexHandler initializationMutex(s_initializationMutex);
// we are lazily creating the pool so if we haven't created our DB Pool
// create it
if (s_pools == NULL)
{
// create our holder for our connection pools
s_pools = new DBConnectionPools();
// now initialize the pools (will be initialized from our configuration)
initializePools();
}
}
// now that we know we have an initialized pool find the pool the user
// is requesting
// first see if we are given a pool name, we may not be (which indicates to use the default)
string name;
if (poolName.length() == 0)
{
// ok we are not given a pool name so use the default pool as specified in the config
name = DBPoolConfiguration::getConfiguration().getDefaultPool();
Debugger::debugger << "No pool name provided, using default of: " << name << "\n";
}
else
{
// the user gave us a pool so use it
name = poolName;
Debugger::debugger << "Using pool " << name << "\n";
}
// now get our pool from the set of pools
DBPool*& pool = (*s_pools)[name];
// if we found the pool then return it to the user
if (pool != NULL)
{
return *pool;
}
else
{
// we didn't find the pool - we have a serious problem here!!
throw DBException(poolName + " is not a valid pool name!");
}
}
//-------------------------------------------------------------------
// Static method that initializes the individual instances of DBPool
// with each representing a distinct connection pool
void DBPool::initializePools(void)
{
// get our list of pools from the config service
vector<string> pools = DBPoolConfiguration::getConfiguration().getPools();
// now iterator our list of pools and initialize each pool
for (vector<string>::iterator it = pools.begin(); it != pools.end(); it++)
{
// create a new instance of DBPool for the given pool (this constructor
// will cause the pool to initialize itself
DBPool* pool = new DBPool(*it);
// now add this to our map of pools
(*s_pools)[*it] = pool;
}
}
//-------------------------------------------------------------------
// Constructor - initializes our pool based on configuration information
//
DBPool::DBPool(void)
: m_poolName(""), m_testFunction(NULL)
{
// this constructor should never be used
}
DBPool::DBPool(const string& poolName)
: m_poolName(poolName), m_testFunction(NULL)
{
Debugger::debugger << "Creating pool " << m_poolName << "\n";
initializePool();
}
//-------------------------------------------------------------------
// Destructor
DBPool::~DBPool()
{
cleanupPool();
}
//-------------------------------------------------------------------
// Initialize our pool of database connections
void DBPool::initializePool(void)
{
// our initial pool size is based on configuration
long initialPoolSize = DBPoolConfiguration::getConfiguration().getInitialPoolSize(m_poolName);
Debugger::debugger << "Initializing connection pool with " << initialPoolSize << " connections" << "\n";
// now create our connections for our initial pool
createConnections(initialPoolSize);
}
//-------------------------------------------------------------------
// Releases all connections in the pool
void DBPool::cleanupPool(void)
{
// go through our set of available connections and release the connections
while (!m_availablePool.empty())
{
DBConnection* connection = m_availablePool.front();
m_availablePool.pop();
DBConnectionFactory::getInstance().releaseConnection(connection);
}
// now clean-up any connections orphaned in our in-use list - this is bad
// but I'm not sure I can
for (InUseConnections::iterator it = m_inUsePool.begin(); it != m_inUsePool.end(); it++)
{
// release our connection
DBConnectionFactory::getInstance().releaseConnection((DBConnection*) (*it));
}
m_inUsePool.clear();
}
//-------------------------------------------------------------------
// Gets a new connection from the pool
DBConnectionPtr DBPool::getConnection(void) throw (DBException)
{
DBConnection* connection = NULL;
// we need to insure serialized access to this method so we don't run
// in to timer problems where multiple people are accessing the connection
// list or this logic simultaneously. So I am using a home-grown mutex to do
// this. I thought STL has one built-in but I could not find it so I ended
// up building my own
MutexHandler mutexHandler(m_availablePoolMutex);
// Note: one thing to note here is I could get the config info outside of the loop
// to avoid overhead that might be associated with getting those values, however,
// I am not doing that to allow the configuration to manage the information such
// that dynamic updates will be immediately reflected, etc...
// set a our start-time for when we started trying to get a connection
time_t startTime = 0;
// we are putting ourselves in a while loop to try and get a connection
// our while condition is 'while(true)' which means, in theory, this could
// be an infinite loop but we have a number of break conditions that will cause
// us to exit the loop and it is those conditions we are looking for like
// getting a valid connection or timing out
while (true)
{
// try and get a connection from the pool
connection = getConnectionFromPool();
// if we got a connection back then see if we should test it
if (connection != NULL)
{
// if we are to validate the connection before returning then validate the
// connection
if (DBPoolConfiguration::getConfiguration().validateConnectionBeforeReturning(m_poolName))
{
// test the connection, if it is not valid then we will release the connection
// and set our reference to bull
if (!testConnection(connection))
{
// bad connection - release this reference and reset our variable
DBConnectionFactory::getInstance().releaseConnection(connection);
connection = NULL;
}
}
// if our connection variable is still valid then we have a connection
// good enough to return to the user
if (connection != NULL)
{
// add this connection to our in-use pool
addInUseConnection(connection);
Debugger::debugger << "Got a connection from the pool" << "\n";
// break out of our loop
return DBConnectionPtr(connection, m_poolName);
}
}
// if we are here then we didn't get a connection on the above try so
// see if we should wait for the connection and if so, have we timed
// out (we are in a loop after all)
if (DBPoolConfiguration::getConfiguration().waitForConnection(m_poolName))
{
// see if we have waited at all yet (our first time through the loop
// we initialize a start time - otherwise we see if we have timed out
if (startTime == 0)
{
// initialize our start time
Debugger::debugger << "Waiting for a connection to become available...." << "\n";
time(&startTime);
}
else
{
// this is not our first time with this so see if we should have expired
// get our wait period
long wait = DBPoolConfiguration::getConfiguration().getConnectionWaitPeriod(m_poolName);
// if our wait period is not infinite we need to see if we have waited
// long enough (in which case we will error out). If it is infinite
/// we just keep waiting for a valid connections
if (wait != DBPoolConfiguration::CONNECTION_WAIT_PERIOD_INFINITE)
{
time_t currentTime;
time(¤tTime);
if (difftime(currentTime, startTime) > wait)
{
Debugger::debugger << "Timed out waiting for a connection to become available" << "\n";
// oh, to bad - we have waited long enough for a connection
// so throw the user out with an error
throw DBException("Failed to obtain a connection from the pool");
}
}
}
}
else
{
// we are not configured to wait for a connection to become available
// so break out without acquiring a connection
Debugger::debugger << "No connection available in pool!" << "\n";
throw DBException("Failed to obtain a connection from the pool!");
}
}
}
//-------------------------------------------------------------------
// Releases a connection back to the pool
void DBPool::releaseConnection(DBConnectionPtr& connection) throw (DBException)
{
Debugger::debugger << "Releasing a connection back to our pool" << "\n";
// since we are using iterators we have to be sure we are thread safe here
// so we are using a mutex to insure only one person is releasing a connection
// at a time - the mutex will automatically release once this method completes
MutexHandler inUsePoolMutexHandler(m_inUsePoolMutex);
// find this connection in our in-use pool
InUseConnections::iterator iterator = m_inUsePool.find((void*)connection.getConnection());
// if we found our element then erase it
if (iterator != m_inUsePool.end())
{
m_inUsePool.erase(iterator);
// now put this connection back in to our available pool
// first, make sure this is a valid connection
if (connection->invalidConnection())
{
// this is an invalid connection so just drop it (we do this by deleting our
// pointer to the connection
DBConnectionFactory::getInstance().releaseConnection((DBConnection*)connection);
}
else
{
// ok, our connection has not been flagged by the caller as being invalid
// so add it back to our pool
m_availablePool.push(connection.getConnection());
}
}
else
{
throw DBException("Failed to find connection in pool - something is very wrong");
}
}
//-------------------------------------------------------------------
// Add connections to our connection pool - this can be used to seed
// our pool or to add additional connections up to our max size
void DBPool::createConnections(long numConnections)
{
Debugger::debugger << "Creating " << numConnections << " connections in pool." << "\n";
unsigned long maxPoolSize = DBPoolConfiguration::getConfiguration().getMaxPoolSize(m_poolName);
// add a number of connections in to our pool
for (int i = 0; i < numConnections; i++)
{
// make sure we have not reached out limit - if we haven't then add
// the new connection
if ((m_availablePool.size() + m_inUsePool.size()) < maxPoolSize)
{
m_availablePool.push(DBConnectionFactory::getInstance().createConnection(m_poolName));
}
else
{
// we are at our max so we cannot add any more connections
Debugger::debugger << "Connection cannot be created, maximum pool size has been reached" << "\n";
break;
}
}
}
//-------------------------------------------------------------------
// Common routine for popping a connection off of the available connection
// pool.
DBConnection* DBPool::getConnectionFromPool(void) throw (DBException)
{
Debugger::debugger << "Getting a connection from the pool..." << "\n";
DBConnection* connection = NULL;
// if our pool is currently empty then try and add some connections to the pool
if (m_availablePool.empty())
{
// try to add some connections to the pool
createConnections(DBPoolConfiguration::getConfiguration().getPoolIncrement(m_poolName));
}
// now try and get a connection from the pool - the pool could still be empty so
// make sure we have available connections before doing this
if (!m_availablePool.empty())
{
// pop a connection off of the pool
connection = (DBConnection*) m_availablePool.front();
m_availablePool.pop();
}
// return our connection
return connection;
}
//-------------------------------------------------------------------
// Adds a connection as an in-use connection
void DBPool::addInUseConnection(DBConnection* connection)
{
// simply add it to our set of in-use connections
m_inUsePool.insert((void*) connection);
}
//-------------------------------------------------------------------
// A debug routine for querying how many connections are availabe in the
// current pool
size_t DBPool::getAvailableConnections(void)
{
return m_availablePool.size();
}
//-------------------------------------------------------------------
// Debug routine for querying how many connections are currently in use
size_t DBPool::getInUseConnections(void)
{
return m_inUsePool.size();
}
//-------------------------------------------------------------------
// Registers a test function with the connection.
// This function will be called whenever the connection needs to
// be tested to insure it is "live"
void DBPool::registerTestFunction(CONNECTIONTEST test)
{
m_testFunction = test;
}
//-------------------------------------------------------------------
// Test the database connection to insure it is valid
bool DBPool::testConnection(DBConnection* connection)
{
// if we have a registered test function for this pool us it
if (m_testFunction != NULL)
{
return m_testFunction(connection);
}
else
{
// else we have no registered test function so just ask the connection
return connection->testConnection();
}
}