Revision 5168
Added by daigle about 15 years ago
src/edu/ucsb/nceas/metacat/database/DBConnectionPool.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package edu.ucsb.nceas.metacat.database; |
29 | 29 |
|
30 |
import java.io.*; |
|
31 | 30 |
import java.util.Vector; |
32 |
import java.lang.*; |
|
33 | 31 |
import java.sql.*; |
34 |
import java.util.Stack; |
|
35 |
import java.util.Hashtable; |
|
36 |
import java.util.Enumeration; |
|
37 | 32 |
|
38 | 33 |
import org.apache.log4j.Logger; |
39 | 34 |
|
... | ... | |
51 | 46 |
|
52 | 47 |
//static attributes |
53 | 48 |
private static DBConnectionPool instance; |
54 |
private static Vector connectionPool; |
|
49 |
private static Vector<DBConnection> connectionPool;
|
|
55 | 50 |
private static Thread runner; |
56 |
private static int countOfReachMaximum = 0; |
|
51 |
private static int _countOfReachMaximum = 0;
|
|
57 | 52 |
private static Logger logMetacat = Logger.getLogger(DBConnectionPool.class); |
58 | 53 |
|
59 |
private static int maxConnNum; |
|
60 |
private static int initConnNum; |
|
61 |
private static int incrConnNum; |
|
62 |
private static long maxAge; |
|
63 |
private static long maxConnTime; |
|
64 |
private static int maxUsageNum; |
|
65 |
private static String dbConnRecyclThrd; |
|
66 |
private static long cyclTimeDbConn; |
|
54 |
private static int _maxConnNum; |
|
55 |
private static int _initConnNum; |
|
56 |
private static int _incrConnNum; |
|
57 |
private static long _maxAge; |
|
58 |
private static long _maxConnTime; |
|
59 |
private static int _maxUsageNum; |
|
60 |
private static int _connCountWarnLimit; |
|
61 |
private static String _dbConnRecyclThrd; |
|
62 |
private static long _cyclTimeDbConn; |
|
67 | 63 |
|
68 | 64 |
final static int MAXIMUMCONNECTIONNUMBER; |
69 | 65 |
final static int INITIALCONNECTIONNUMBER; |
... | ... | |
75 | 71 |
final static long CYCLETIMEOFDBCONNECTION; |
76 | 72 |
|
77 | 73 |
static { |
78 |
int maxConnNum = 0; |
|
79 |
int initConnNum = 0; |
|
80 |
int incrConnNum = 0; |
|
81 |
long maxAge = 0; |
|
82 |
long maxConnTime = 0; |
|
83 |
int maxUsageNum = 0; |
|
84 |
String dbConnRecyclThrd = null; |
|
85 |
long cyclTimeDbConn = 0; |
|
74 |
// int maxConnNum = 0; |
|
75 |
// int initConnNum = 0; |
|
76 |
// int incrConnNum = 0; |
|
77 |
// long maxAge = 0; |
|
78 |
// long maxConnTime = 0; |
|
79 |
// int maxUsageNum = 0; |
|
80 |
// int connCountWarnLevel = 0; |
|
81 |
// String dbConnRecyclThrd = null; |
|
82 |
// long cyclTimeDbConn = 0; |
|
86 | 83 |
|
87 | 84 |
try { |
88 | 85 |
// maximum connection number in the connection pool |
89 |
maxConnNum = Integer.parseInt(PropertyService |
|
86 |
_maxConnNum = Integer.parseInt(PropertyService
|
|
90 | 87 |
.getProperty("database.maximumConnections")); |
91 |
initConnNum = Integer.parseInt(PropertyService |
|
88 |
_initConnNum = Integer.parseInt(PropertyService
|
|
92 | 89 |
.getProperty("database.initialConnections")); |
93 |
incrConnNum = Integer.parseInt(PropertyService |
|
90 |
_incrConnNum = Integer.parseInt(PropertyService
|
|
94 | 91 |
.getProperty("database.incrementConnections")); |
95 |
maxAge = Integer.parseInt(PropertyService |
|
92 |
_maxAge = Integer.parseInt(PropertyService
|
|
96 | 93 |
.getProperty("database.maximumConnectionAge")); |
97 |
maxConnTime = Long.parseLong(PropertyService |
|
94 |
_maxConnTime = Long.parseLong(PropertyService
|
|
98 | 95 |
.getProperty("database.maximumConnectionTime")); |
99 |
maxUsageNum = Integer.parseInt(PropertyService |
|
96 |
_maxUsageNum = Integer.parseInt(PropertyService
|
|
100 | 97 |
.getProperty("database.maximumUsageNumber")); |
101 |
dbConnRecyclThrd = PropertyService |
|
98 |
_connCountWarnLimit = Integer.parseInt(PropertyService |
|
99 |
.getProperty("database.connectionCountWarnLimit")); |
|
100 |
_dbConnRecyclThrd = PropertyService |
|
102 | 101 |
.getProperty("database.runDBConnectionRecycleThread"); |
103 |
cyclTimeDbConn = Long.parseLong(PropertyService |
|
102 |
_cyclTimeDbConn = Long.parseLong(PropertyService
|
|
104 | 103 |
.getProperty("database.cycleTimeOfDBConnection")); |
105 | 104 |
} catch (PropertyNotFoundException pnfe) { |
106 | 105 |
System.err.println("Could not get property in static block: " |
107 | 106 |
+ pnfe.getMessage()); |
108 | 107 |
} |
109 | 108 |
|
110 |
MAXIMUMCONNECTIONNUMBER = maxConnNum; |
|
111 |
INITIALCONNECTIONNUMBER = initConnNum; |
|
112 |
INCREASECONNECTIONNUMBER = incrConnNum; |
|
113 |
MAXIMUMAGE = maxAge; |
|
114 |
MAXIMUMCONNECTIONTIME = maxConnTime; |
|
115 |
MAXIMUMUSAGENUMBER = maxUsageNum; |
|
116 |
DBCONNECTIONRECYCLETHREAD = dbConnRecyclThrd; |
|
117 |
CYCLETIMEOFDBCONNECTION = cyclTimeDbConn; |
|
109 |
MAXIMUMCONNECTIONNUMBER = _maxConnNum;
|
|
110 |
INITIALCONNECTIONNUMBER = _initConnNum;
|
|
111 |
INCREASECONNECTIONNUMBER = _incrConnNum;
|
|
112 |
MAXIMUMAGE = _maxAge;
|
|
113 |
MAXIMUMCONNECTIONTIME = _maxConnTime;
|
|
114 |
MAXIMUMUSAGENUMBER = _maxUsageNum;
|
|
115 |
DBCONNECTIONRECYCLETHREAD = _dbConnRecyclThrd;
|
|
116 |
CYCLETIMEOFDBCONNECTION = _cyclTimeDbConn;
|
|
118 | 117 |
} |
119 | 118 |
|
120 | 119 |
// the number for trying to check out a connection in the pool in |
... | ... | |
122 | 121 |
final static int LIMIT = 2; |
123 | 122 |
|
124 | 123 |
final static int FREE = 0; //status of a connection |
125 |
final static int BUSY = 1; //statis of a connection
|
|
124 |
final static int BUSY = 1; //status of a connection
|
|
126 | 125 |
/** |
127 | 126 |
* Returns the single instance, creating one if it's the |
128 | 127 |
* first time this method is called. |
... | ... | |
133 | 132 |
if (instance == null) { |
134 | 133 |
instance = new DBConnectionPool(); |
135 | 134 |
Logger log = Logger.getLogger(DBConnectionPool.class); |
136 |
log.debug("MaximumConnectionNumber: "+MAXIMUMCONNECTIONNUMBER);
|
|
137 |
log.debug("Intial connection number: "+INITIALCONNECTIONNUMBER);
|
|
138 |
log.debug("Increated connection Number: "+INCREASECONNECTIONNUMBER);
|
|
139 |
log.debug("Maximum connection age: "+MAXIMUMAGE);
|
|
140 |
log.debug("Maximum connection time: "+MAXIMUMCONNECTIONTIME);
|
|
141 |
log.debug("Maximum usage count: "+MAXIMUMUSAGENUMBER);
|
|
142 |
log.debug("Running recycle thread or not: "+DBCONNECTIONRECYCLETHREAD);
|
|
143 |
log.debug("Cycle time of recycle: "+CYCLETIMEOFDBCONNECTION);
|
|
135 |
log.debug("DBConnectionPool.getInstance - MaximumConnectionNumber: " + MAXIMUMCONNECTIONNUMBER);
|
|
136 |
log.debug("DBConnectionPool.getInstance - Intial connection number: " + INITIALCONNECTIONNUMBER);
|
|
137 |
log.debug("DBConnectionPool.getInstance - Increated connection Number: " + INCREASECONNECTIONNUMBER);
|
|
138 |
log.debug("DBConnectionPool.getInstance - Maximum connection age: " + MAXIMUMAGE);
|
|
139 |
log.debug("DBConnectionPool.getInstance - Maximum connection time: " + MAXIMUMCONNECTIONTIME);
|
|
140 |
log.debug("DBConnectionPool.getInstance - Maximum usage count: " + MAXIMUMUSAGENUMBER);
|
|
141 |
log.debug("DBConnectionPool.getInstance - Running recycle thread or not: " + DBCONNECTIONRECYCLETHREAD);
|
|
142 |
log.debug("DBConnectionPool.getInstance - Cycle time of recycle: " + CYCLETIMEOFDBCONNECTION);
|
|
144 | 143 |
} |
145 | 144 |
return instance; |
146 | 145 |
} |
... | ... | |
152 | 151 |
|
153 | 152 |
private DBConnectionPool() throws SQLException |
154 | 153 |
{ |
155 |
connectionPool = new Vector(); |
|
154 |
connectionPool = new Vector<DBConnection>();
|
|
156 | 155 |
initialDBConnectionPool(); |
157 | 156 |
//running the thread to recycle DBConnection |
158 | 157 |
if (DBCONNECTIONRECYCLETHREAD.equals("on")) |
... | ... | |
225 | 224 |
int random = 0; //random number |
226 | 225 |
int index = 0; //index |
227 | 226 |
int size = 0; //size of connection pool |
228 |
logMetacat.debug("Try to checking out connection...");
|
|
227 |
logMetacat.debug("DBConnectionPool.getDBConnection - Trying to check out connection...");
|
|
229 | 228 |
size = connectionPool.size(); |
230 |
logMetacat.debug("size of connection pool: "+size);
|
|
229 |
logMetacat.debug("DBConnectionPool.getDBConnection - size of connection pool: " + size);
|
|
231 | 230 |
|
232 | 231 |
//try every DBConnection in the pool |
233 | 232 |
//every DBConnection will be try LIMITE times |
... | ... | |
240 | 239 |
{ |
241 | 240 |
index =(i+random)%size; |
242 | 241 |
db = (DBConnection) connectionPool.elementAt(index); |
243 |
logMetacat.debug("Index: "+index);
|
|
244 |
logMetacat.debug("Tag: "+db.getTag());
|
|
245 |
logMetacat.debug("Status: "+db.getStatus());
|
|
242 |
logMetacat.debug("DBConnectionPool.getDBConnection - Index: " + index);
|
|
243 |
logMetacat.debug("DBConnectionPool.getDBConnection - Tag: " + db.getTag());
|
|
244 |
logMetacat.debug("DBConnectionPool.getDBConnection - Status: " + db.getStatus());
|
|
246 | 245 |
//check if the connection is free |
247 | 246 |
if (db.getStatus()==FREE) |
248 | 247 |
{ |
... | ... | |
260 | 259 |
db.setCheckOutMethodName(methodName); |
261 | 260 |
db.setAutoCommit(true); |
262 | 261 |
//debug message |
263 |
logMetacat.debug("The connection is checked out: " |
|
264 |
+db.getTag()); |
|
265 |
logMetacat.debug("The method for checking is: " |
|
266 |
+db.getCheckOutMethodName()); |
|
267 |
logMetacat.debug("The age is "+db.getAge()); |
|
268 |
logMetacat.debug("The usage is "+db.getUsageCount()); |
|
269 |
logMetacat.debug("The conection time it has: " |
|
270 |
+db.getConnectionTime()); |
|
262 |
logMetacat.debug("DBConnectionPool.getDBConnection - The connection is checked out: " + db.getTag()); |
|
263 |
logMetacat.debug("DBConnectionPool.getDBConnection - The method for checking is: " + db.getCheckOutMethodName()); |
|
264 |
logMetacat.debug("DBConnectionPool.getDBConnection - The age is " + db.getAge()); |
|
265 |
logMetacat.debug("DBConnectionPool.getDBConnection - The usage is " + db.getUsageCount()); |
|
266 |
logMetacat.debug("DBConnectionPool.getDBConnection - The connection time is: " + db.getConnectionTime()); |
|
271 | 267 |
//set check out time |
272 | 268 |
db.setCheckOutTime(System.currentTimeMillis()); |
273 | 269 |
// set count of reach maximum 0 because it can check out |
274 |
countOfReachMaximum =0;
|
|
270 |
_countOfReachMaximum = 0;
|
|
275 | 271 |
return db; |
276 | 272 |
}//if |
277 | 273 |
else//The DBConnection has some problem |
... | ... | |
293 | 289 |
|
294 | 290 |
if ( size < MAXIMUMCONNECTIONNUMBER ) |
295 | 291 |
{ |
296 |
if ((size+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
|
|
292 |
if ((size + INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
|
|
297 | 293 |
{ |
298 | 294 |
//if we can create INCREASECONNECTIONNUMBER of new DBConnection |
299 | 295 |
//add to connection pool |
300 |
for ( int i=0; i<INCREASECONNECTIONNUMBER; i++)
|
|
296 |
for ( int i = 0; i < INCREASECONNECTIONNUMBER; i++)
|
|
301 | 297 |
{ |
302 | 298 |
DBConnection dbConn = new DBConnection(); |
303 | 299 |
connectionPool.add(dbConn); |
... | ... | |
321 | 317 |
" open db connections is reached." + |
322 | 318 |
" New db connection to MetaCat" + |
323 | 319 |
" cannot be established.");*/ |
324 |
logMetacat.fatal("The maximum of " +MAXIMUMCONNECTIONNUMBER + |
|
325 |
" open db connections is reached." + |
|
326 |
" New db connection to MetaCat" + |
|
327 |
" cannot be established."); |
|
328 |
countOfReachMaximum ++; |
|
329 |
if (countOfReachMaximum >= 10) |
|
320 |
logMetacat.fatal("DBConnectionPool.getDBConnection - The maximum of " + MAXIMUMCONNECTIONNUMBER + |
|
321 |
" open db connections is reached. New db connection to MetaCat" + |
|
322 |
" cannot be established."); |
|
323 |
_countOfReachMaximum ++; |
|
324 |
if (_countOfReachMaximum >= 10) |
|
330 | 325 |
{ |
331 |
countOfReachMaximum =0; |
|
326 |
_countOfReachMaximum =0;
|
|
332 | 327 |
logMetacat.fatal("finally could not get dbconnection"); |
333 | 328 |
return null; |
334 | 329 |
} |
... | ... | |
337 | 332 |
//if couldn't get a connection, sleep 20 seconds and try again. |
338 | 333 |
try |
339 | 334 |
{ |
340 |
logMetacat.info("sleep for could not get dbconnection");
|
|
335 |
logMetacat.debug("DBConnectionPool.getDBConnection - sleep 5000ms, could not get dbconnection");
|
|
341 | 336 |
Thread.sleep(5000); |
342 | 337 |
} |
343 | 338 |
catch (Exception e) |
344 | 339 |
{ |
345 |
logMetacat.error("Exception in getDBConnection" + e.getMessage());
|
|
340 |
logMetacat.error("DBConnectionPool.getDBConnection - General exception: " + e.getMessage());
|
|
346 | 341 |
} |
347 | 342 |
} |
348 | 343 |
|
... | ... | |
352 | 347 |
//recursive to get new connection |
353 | 348 |
return getDBConnection(methodName); |
354 | 349 |
}//getDBConnection |
355 |
|
|
356 | 350 |
|
357 |
|
|
358 |
|
|
359 | 351 |
/** |
360 | 352 |
* Method to check if a db connection works fine or not |
361 | 353 |
* Check points include: |
... | ... | |
367 | 359 |
* @param dbConn, the DBConnection object need to check |
368 | 360 |
*/ |
369 | 361 |
private static boolean validateDBConnection (DBConnection dbConn) |
370 |
{ |
|
371 |
|
|
372 |
|
|
362 |
{ |
|
373 | 363 |
//Check if the DBConnection usageCount if it is too many |
374 | 364 |
if (dbConn.getUsageCount() >= MAXIMUMUSAGENUMBER ) |
375 | 365 |
{ |
376 |
logMetacat.debug("Connection usageCount is too many: "+
|
|
366 |
logMetacat.debug("DBConnectionPool.validateDBConnection - Connection usageCount is too high: "+
|
|
377 | 367 |
dbConn.getUsageCount()); |
378 | 368 |
return false; |
379 | 369 |
} |
... | ... | |
381 | 371 |
//Check if the DBConnection has too much connection time |
382 | 372 |
if (dbConn.getConnectionTime() >= MAXIMUMCONNECTIONTIME) |
383 | 373 |
{ |
384 |
logMetacat.debug("Connection has too much connection time: "+
|
|
385 |
dbConn.getConnectionTime()); |
|
374 |
logMetacat.debug("DBConnectionPool.validateDBConnection - Connection has too much connection time: " +
|
|
375 |
dbConn.getConnectionTime());
|
|
386 | 376 |
return false; |
387 | 377 |
} |
388 | 378 |
|
389 | 379 |
//Check if the DBConnection is too old |
390 | 380 |
if (dbConn.getAge() >=MAXIMUMAGE) |
391 | 381 |
{ |
392 |
logMetacat.debug("Connection is too old: "+dbConn.getAge());
|
|
382 |
logMetacat.debug("DBConnectionPool.validateDBConnection - Connection is too old: " + dbConn.getAge());
|
|
393 | 383 |
return false; |
394 | 384 |
} |
395 | 385 |
|
... | ... | |
407 | 397 |
} |
408 | 398 |
catch (Exception e) |
409 | 399 |
{ |
410 |
logMetacat.error("Error in validateDBConnection: " |
|
411 |
+e.getMessage()); |
|
400 |
logMetacat.error("DBConnectionPool.validateDBConnection - General error:" + e.getMessage()); |
|
412 | 401 |
return false; |
413 | 402 |
} |
414 | 403 |
|
... | ... | |
429 | 418 |
index = getIndexOfPoolForConnection(conn); |
430 | 419 |
if ( index ==-1 ) |
431 | 420 |
{ |
432 |
logMetacat.info("Couldn't find a DBConnection in the pool" |
|
433 |
+" which have same tag to the returned" |
|
434 |
+" DBConnetion object"); |
|
421 |
logMetacat.info("DBConnectionPool.returnDBConnection - Couldn't find a DBConnection in the pool" + |
|
422 |
" which have same tag to the returned DBConnetion object"); |
|
435 | 423 |
return; |
436 | 424 |
|
437 | 425 |
}//if |
438 | 426 |
else |
439 | 427 |
{ |
440 |
//check the paramter - serialNumber which will be keep in calling method |
|
441 |
//if it is as same as the object's checkoutserial number. |
|
428 |
//check the parameter - serialNumber which will be keep in calling method
|
|
429 |
//if it is as same as the object's checkout serial number.
|
|
442 | 430 |
//if it is same return it. If it is not same, maybe the connection already |
443 |
// was returned ealier. |
|
444 |
logMetacat.debug("serial number in Connection: "
|
|
445 |
+conn.getCheckOutSerialNumber());
|
|
446 |
logMetacat.debug("serial number in local: "+serialNumber);
|
|
431 |
// was returned earlier.
|
|
432 |
logMetacat.debug("DBConnectionPool.returnDBConnection - serial number in Connection: " +
|
|
433 |
conn.getCheckOutSerialNumber()); |
|
434 |
logMetacat.debug("DBConnectionPool.returnDBConnection - serial number in local: " + serialNumber);
|
|
447 | 435 |
if (conn.getCheckOutSerialNumber() == serialNumber) |
448 | 436 |
{ |
449 | 437 |
dbConn = (DBConnection) connectionPool.elementAt(index); |
... | ... | |
456 | 444 |
//set check out time to 0 |
457 | 445 |
dbConn.setCheckOutTime(0); |
458 | 446 |
|
459 |
logMetacat.debug("Connection: "+dbConn.getTag()+" checked in."); |
|
460 |
logMetacat.debug("Connection: "+dbConn.getTag()+"'s status: " |
|
461 |
+dbConn.getStatus()); |
|
447 |
logMetacat.debug("DBConnectionPool.returnDBConnection - Connection: " + |
|
448 |
dbConn.getTag() + " checked in."); |
|
449 |
logMetacat.debug("DBConnectionPool.returnDBConnection - Connection: " + |
|
450 |
dbConn.getTag() + "'s status: " + dbConn.getStatus()); |
|
462 | 451 |
|
463 | 452 |
}//if |
464 | 453 |
else |
465 | 454 |
{ |
466 |
logMetacat.info("This DBConnection couldn't return"); |
|
455 |
logMetacat.info("DBConnectionPool.returnDBConnection - This DBConnection couldn't return" + |
|
456 |
dbConn.getTag()); |
|
467 | 457 |
}//else |
468 | 458 |
}//else |
469 | 459 |
|
... | ... | |
512 | 502 |
public static void release() |
513 | 503 |
{ |
514 | 504 |
|
515 |
//shut down the backgroud recycle thread |
|
505 |
//shut down the background recycle thread
|
|
516 | 506 |
if (DBCONNECTIONRECYCLETHREAD.equals("on")) |
517 | 507 |
{ |
518 | 508 |
runner.interrupt(); |
519 | 509 |
} |
520 |
//cose every dbconnection in the pool |
|
510 |
//close every dbconnection in the pool
|
|
521 | 511 |
synchronized(connectionPool) |
522 | 512 |
{ |
523 | 513 |
for (int i=0;i<connectionPool.size();i++) |
... | ... | |
529 | 519 |
}//try |
530 | 520 |
catch (SQLException e) |
531 | 521 |
{ |
532 |
logMetacat.error("Error in release connection: " |
|
522 |
logMetacat.error("DBConnectionPool.release - Error in release connection: "
|
|
533 | 523 |
+e.getMessage()); |
534 | 524 |
}//catch |
535 | 525 |
}//for |
... | ... | |
557 | 547 |
if (dbConn.getStatus()==BUSY && |
558 | 548 |
(System.currentTimeMillis()-dbConn.getCheckOutTime())>=30000) |
559 | 549 |
{ |
560 |
logMetacat.fatal("This DBConnection is checked out for: " |
|
561 |
+(System.currentTimeMillis()-dbConn.getCheckOutTime())/1000 |
|
562 |
+" secs"); |
|
563 |
logMetacat.fatal(dbConn.getTag()); |
|
564 |
logMetacat.error("method: " |
|
565 |
+dbConn.getCheckOutMethodName()); |
|
550 |
logMetacat.fatal("DBConnectionPool.run - This DBConnection is checked out for: " + |
|
551 |
(System.currentTimeMillis()-dbConn.getCheckOutTime())/1000 + " secs"); |
|
552 |
logMetacat.fatal("DBConnectionPool.run - " + dbConn.getTag()); |
|
553 |
logMetacat.error("DBConnectionPool.run - method: " + dbConn.getCheckOutMethodName()); |
|
566 | 554 |
|
567 | 555 |
} |
568 | 556 |
|
... | ... | |
574 | 562 |
//try to print out the warning message for every connection |
575 | 563 |
if (dbConn.getWarningMessage()!=null) |
576 | 564 |
{ |
577 |
logMetacat.warn("Warning for connection "
|
|
578 |
+dbConn.getTag()+" : "+ dbConn.getWarningMessage());
|
|
565 |
logMetacat.warn("DBConnectionPool.run - Warning for connection " +
|
|
566 |
dbConn.getTag() + " : " + dbConn.getWarningMessage());
|
|
579 | 567 |
} |
580 | 568 |
//check if it is valiate, if not create new one and replace old one |
581 | 569 |
if (!validateDBConnection(dbConn)) |
582 | 570 |
{ |
583 |
logMetacat.debug("Recyle it: "+ dbConn.getTag());
|
|
571 |
logMetacat.debug("DBConnectionPool.run - Recyle: " + dbConn.getTag());
|
|
584 | 572 |
//close this DBConnection |
585 | 573 |
dbConn.close(); |
586 | 574 |
//remove it form connection pool |
... | ... | |
592 | 580 |
}//try |
593 | 581 |
catch (SQLException e) |
594 | 582 |
{ |
595 |
logMetacat.error("Error in DBConnectionPool.run: " |
|
596 |
+e.getMessage()); |
|
583 |
logMetacat.error("DBConnectionPool.run - SQL error: " + e.getMessage()); |
|
597 | 584 |
}//catch |
598 | 585 |
}//if |
599 | 586 |
}//for |
... | ... | |
605 | 592 |
} |
606 | 593 |
catch (Exception e) |
607 | 594 |
{ |
608 |
logMetacat.error("Error in DBConnectionPool.run: " |
|
609 |
+e.getMessage()); |
|
595 |
logMetacat.error("DBConnectionPool.run - General error: " + e.getMessage()); |
|
610 | 596 |
} |
611 | 597 |
}//while |
612 | 598 |
}//run |
... | ... | |
614 | 600 |
/** |
615 | 601 |
* Method to get the number of free DBConnection in DBConnection pool |
616 | 602 |
*/ |
617 |
public static synchronized int getFreeDBConnectionNumber()
|
|
603 |
private static synchronized int getFreeDBConnectionNumber()
|
|
618 | 604 |
{ |
619 | 605 |
int numberOfFreeDBConnetion = 0; //return number |
620 | 606 |
DBConnection db = null; //single DBconnection |
... | ... | |
636 | 622 |
return numberOfFreeDBConnetion; |
637 | 623 |
}//getFreeDBConnectionNumber |
638 | 624 |
|
639 |
/** |
|
640 |
* Method to print out the method name which have busy DBconnection |
|
641 |
*/ |
|
642 |
public void printMethodNameHavingBusyDBConnection() |
|
643 |
{ |
|
644 |
|
|
645 |
DBConnection db = null; //single DBconnection |
|
646 |
int poolSize = 0; //size of connection pool |
|
647 |
//get the size of DBConnection pool |
|
648 |
poolSize = connectionPool.size(); |
|
649 |
//Check every DBConnection in the pool |
|
650 |
for ( int i=0; i<poolSize; i++) |
|
651 |
{ |
|
652 |
|
|
653 |
db = (DBConnection) connectionPool.elementAt(i); |
|
654 |
//check the status of db. If it is free, count it |
|
655 |
if (db.getStatus() == BUSY) |
|
656 |
{ |
|
657 |
logMetacat.warn("This method having a busy DBConnection: " |
|
658 |
+db.getCheckOutMethodName()); |
|
659 |
logMetacat.warn("The busy DBConnection tag is: " |
|
660 |
+db.getTag()); |
|
661 |
}//if |
|
662 |
}//for |
|
625 |
/** |
|
626 |
* Print a list of busy connections. This method should be called when the |
|
627 |
* used connection count exceeds the _connCountWarnLimit set in |
|
628 |
* metacat.properties. |
|
629 |
* |
|
630 |
* @param usedConnectionCount |
|
631 |
* the current count of busy connections |
|
632 |
*/ |
|
633 |
private static void printBusyDBConnections(int usedConnectionCount) { |
|
634 |
boolean showCountWarning = usedConnectionCount > _connCountWarnLimit; |
|
635 |
|
|
636 |
String warnMessage = ""; |
|
637 |
|
|
638 |
// Check every DBConnection in the pool |
|
639 |
for (DBConnection dbConnection : connectionPool) { |
|
640 |
// check the status of db. If it is busy, add it to the message |
|
641 |
if (dbConnection.getStatus() == BUSY) { |
|
642 |
if (showCountWarning) { |
|
643 |
warnMessage += "\n --- Method: " + dbConnection.getCheckOutMethodName() + |
|
644 |
" is using: " + dbConnection.getTag() + " for " + dbConnection.getAge() + " ms "; |
|
645 |
} |
|
646 |
|
|
647 |
if (dbConnection.getConnectionTime() > _maxConnTime) { |
|
648 |
logMetacat.warn("DBConnectionPool.printBusyDBConnections - Excessive connection time, method: " + |
|
649 |
dbConnection.getCheckOutMethodName() + " is using: " + dbConnection.getTag() + |
|
650 |
" for " + dbConnection.getConnectionTime() + " ms "); |
|
651 |
} |
|
652 |
}// if |
|
653 |
}// for |
|
654 |
|
|
655 |
if (showCountWarning) { |
|
656 |
logMetacat.warn("DBConnectionPool.printBusyDBConnections - " + usedConnectionCount + |
|
657 |
" DB connections currently busy because: " + warnMessage); |
|
658 |
} |
|
659 |
} |
|
663 | 660 |
|
664 |
}//printMethodNameHavingBusyDBConnection |
|
665 |
|
|
666 | 661 |
/** |
667 |
* Method to decrease dbconnection pool size when all dbconnections are idle
|
|
668 |
* If all connections are free and connection pool size greater than
|
|
669 |
* initial value, shrink connection pool size to intital value
|
|
670 |
*/
|
|
662 |
* Method to decrease dbconnection pool size when all dbconnections are idle
|
|
663 |
* If all connections are free and connection pool size greater than initial
|
|
664 |
* value, shrink connection pool size to initial value
|
|
665 |
*/
|
|
671 | 666 |
public static synchronized boolean shrinkConnectionPoolSize() |
672 | 667 |
{ |
673 | 668 |
int connectionPoolSize = 0; //store the number of dbconnection pool size |
674 | 669 |
int freeConnectionSize = 0; //store the number of free dbconnection in pool |
675 | 670 |
int difference = 0; // store the difference number between connection size |
676 | 671 |
// and free connection |
677 |
boolean hasException = false; //to check if has a exception happend |
|
672 |
boolean hasException = false; //to check if has a exception happened
|
|
678 | 673 |
boolean result = false; //result |
679 | 674 |
DBConnection conn = null; // the dbconnection |
680 | 675 |
connectionPoolSize = connectionPool.size(); |
... | ... | |
682 | 677 |
difference = connectionPoolSize - freeConnectionSize; |
683 | 678 |
|
684 | 679 |
if(freeConnectionSize < connectionPoolSize){ |
685 |
logMetacat.warn(difference + " connection(s) " +
|
|
686 |
"being used and connection pool size is " +connectionPoolSize); |
|
680 |
logMetacat.info("DBConnectionPool.shrinkConnectionPoolSize - " + difference + " connection(s) " +
|
|
681 |
"being used and connection pool size is " + connectionPoolSize);
|
|
687 | 682 |
} else { |
688 |
logMetacat.debug("Connection pool size: " +connectionPoolSize);
|
|
689 |
logMetacat.debug("Free Connection number: "+freeConnectionSize);
|
|
683 |
logMetacat.info("DBConnectionPool.shrinkConnectionPoolSize - Connection pool size: " + connectionPoolSize);
|
|
684 |
logMetacat.info("DBConnectionPool.shrinkConnectionPoolSize - Free Connection number: " + freeConnectionSize);
|
|
690 | 685 |
} |
691 | 686 |
|
692 | 687 |
//If all connections are free and connection pool size greater than |
... | ... | |
708 | 703 |
}//try |
709 | 704 |
catch (SQLException e) |
710 | 705 |
{ |
711 |
// set hadException ture
|
|
706 |
// set hadException true
|
|
712 | 707 |
hasException = true; |
713 |
logMetacat.error("Couldn't close a DBConnection in " + |
|
714 |
"DBConnectionPool.shrinkDBConnectionPoolSize: " + |
|
715 |
e.getMessage()); |
|
708 |
logMetacat.error("DBConnectionPool.shrinkConnectionPoolSize - SQL Exception: " + e.getMessage()); |
|
716 | 709 |
}//catch |
717 | 710 |
|
718 | 711 |
//remove it from pool |
719 | 712 |
connectionPool.remove(i); |
720 |
// becuase enter the loop, set result true
|
|
713 |
// because enter the loop, set result true
|
|
721 | 714 |
result = true; |
722 | 715 |
}//for |
723 | 716 |
}//if |
... | ... | |
741 | 734 |
{ |
742 | 735 |
int connectionPoolSize = 0; //store the number of dbconnection pool size |
743 | 736 |
int freeConnectionSize = 0; //store the number of free dbconnection in pool |
744 |
int difference = 0; // store the difference number between connection size
|
|
737 |
int usedConnectionCount = 0; // store the difference number between connection size
|
|
745 | 738 |
// and free connection |
746 | 739 |
|
747 | 740 |
DBConnection conn = null; // the dbconnection |
748 | 741 |
connectionPoolSize = connectionPool.size(); |
749 | 742 |
freeConnectionSize = getFreeDBConnectionNumber(); |
750 |
difference = connectionPoolSize - freeConnectionSize;
|
|
743 |
usedConnectionCount = connectionPoolSize - freeConnectionSize;
|
|
751 | 744 |
|
745 |
printBusyDBConnections(usedConnectionCount); |
|
746 |
|
|
752 | 747 |
if(freeConnectionSize < connectionPoolSize){ |
753 |
logMetacat.warn(difference + " connection(s) " +
|
|
754 |
"being used and connection pool size is " +connectionPoolSize);
|
|
748 |
logMetacat.info("DBConnectionPool.shrinkDBConnectionPoolSize - " + usedConnectionCount + " connection(s) " +
|
|
749 |
"being used and connection pool size is " + connectionPoolSize);
|
|
755 | 750 |
} else { |
756 |
logMetacat.debug("Connection pool size: " +connectionPoolSize); |
|
757 |
logMetacat.debug("Free Connection number: "+freeConnectionSize); |
|
751 |
logMetacat.debug("DBConnectionPool.shrinkDBConnectionPoolSize - " + |
|
752 |
"Connection pool size: " + connectionPoolSize); |
|
753 |
logMetacat.debug("DBConnectionPool.shrinkDBConnectionPoolSize - " + |
|
754 |
"Free Connection number: " + freeConnectionSize); |
|
758 | 755 |
} |
759 | 756 |
|
760 | 757 |
//If all connections are free and connection pool size greater than |
761 | 758 |
//initial value, shrink connection pool size to intital value |
762 |
if (difference == 0 && connectionPoolSize > INITIALCONNECTIONNUMBER)
|
|
759 |
if (usedConnectionCount == 0 && connectionPoolSize > INITIALCONNECTIONNUMBER)
|
|
763 | 760 |
{ |
764 | 761 |
//db connection having index from to connectionpoolsize -1 |
765 | 762 |
//intialConnectionnumber should be close and remove from pool |
... | ... | |
779 | 776 |
catch (SQLException e) |
780 | 777 |
{ |
781 | 778 |
|
782 |
logMetacat.error("Couldn't close a DBConnection in " + |
|
783 |
"DBConnectionPool.shrinkDBConnectionPoolSize: " + |
|
784 |
e.getMessage()); |
|
779 |
logMetacat.error("DBConnectionPool.shrinkDBConnectionPoolSize - SQL error: " + e.getMessage()); |
|
785 | 780 |
}//catch |
786 | 781 |
|
787 | 782 |
//remove it from pool |
Also available in: Unified diff
log connection warnings only when thresholds are exceeded. Add method names to log output