Revision 2663
Added by sgarg over 18 years ago
DBConnectionPool.java | ||
---|---|---|
36 | 36 |
import java.util.Hashtable; |
37 | 37 |
import java.util.Enumeration; |
38 | 38 |
|
39 |
import org.apache.log4j.Logger; |
|
40 |
|
|
39 | 41 |
/** |
40 | 42 |
* A class represent a DBConnection pool. Another user can use the |
41 | 43 |
* object to initial a connection pool, get db connection or return it. |
... | ... | |
50 | 52 |
private static Vector connectionPool; |
51 | 53 |
private static Thread runner; |
52 | 54 |
private static int countOfReachMaximum = 0; |
53 |
|
|
55 |
private static Logger logMetacat = Logger.getLogger(DBConnectionPool.class); |
|
56 |
|
|
54 | 57 |
//maximum connection number in the connection pool |
55 | 58 |
final static int MAXIMUMCONNECTIONNUMBER= |
56 | 59 |
Integer.parseInt(MetaCatUtil.getOption("maximumConnections")); |
... | ... | |
92 | 95 |
public static synchronized DBConnectionPool getInstance() |
93 | 96 |
throws SQLException |
94 | 97 |
{ |
95 |
MetaCatUtil.debugMessage
|
|
96 |
("MaximumConnectionNumber: "+MAXIMUMCONNECTIONNUMBER, 49);
|
|
97 |
MetaCatUtil.debugMessage
|
|
98 |
("Intial connection number: "+INITIALCONNECTIONNUMBER, 49);
|
|
99 |
MetaCatUtil.debugMessage
|
|
100 |
("Increated connection Number: "+INCREASECONNECTIONNUMBER, 49);
|
|
101 |
MetaCatUtil.debugMessage
|
|
102 |
("Maximum connection age: "+MAXIMUMAGE, 49);
|
|
103 |
MetaCatUtil.debugMessage
|
|
104 |
("Maximum connection time: "+MAXIMUMCONNECTIONTIME, 49);
|
|
105 |
MetaCatUtil.debugMessage
|
|
106 |
("Maximum usage count: "+MAXIMUMUSAGENUMBER, 49);
|
|
107 |
MetaCatUtil.debugMessage
|
|
108 |
("Running recycle thread or not: "+DBCONNECTIONRECYCLETHREAD, 49);
|
|
109 |
MetaCatUtil.debugMessage
|
|
110 |
("Cycle time of recycle: "+CYCLETIMEOFDBCONNECTION, 49);
|
|
98 |
MetaCatUtil.printMessage
|
|
99 |
("MaximumConnectionNumber: "+MAXIMUMCONNECTIONNUMBER); |
|
100 |
MetaCatUtil.printMessage
|
|
101 |
("Intial connection number: "+INITIALCONNECTIONNUMBER); |
|
102 |
MetaCatUtil.printMessage
|
|
103 |
("Increated connection Number: "+INCREASECONNECTIONNUMBER); |
|
104 |
MetaCatUtil.printMessage
|
|
105 |
("Maximum connection age: "+MAXIMUMAGE); |
|
106 |
MetaCatUtil.printMessage
|
|
107 |
("Maximum connection time: "+MAXIMUMCONNECTIONTIME); |
|
108 |
MetaCatUtil.printMessage
|
|
109 |
("Maximum usage count: "+MAXIMUMUSAGENUMBER); |
|
110 |
MetaCatUtil.printMessage
|
|
111 |
("Running recycle thread or not: "+DBCONNECTIONRECYCLETHREAD); |
|
112 |
MetaCatUtil.printMessage
|
|
113 |
("Cycle time of recycle: "+CYCLETIMEOFDBCONNECTION); |
|
111 | 114 |
if (instance == null) { |
112 | 115 |
instance = new DBConnectionPool(); |
113 | 116 |
} |
... | ... | |
194 | 197 |
int random = 0; //random number |
195 | 198 |
int index = 0; //index |
196 | 199 |
int size = 0; //size of connection pool |
197 |
MetaCatUtil.debugMessage("Try to checking out connection...", 65);
|
|
200 |
logMetacat.debug("Try to checking out connection...");
|
|
198 | 201 |
size = connectionPool.size(); |
199 |
MetaCatUtil.debugMessage("size of connection pool: "+size, 55);
|
|
202 |
logMetacat.debug("size of connection pool: "+size);
|
|
200 | 203 |
|
201 | 204 |
//try every DBConnection in the pool |
202 | 205 |
//every DBConnection will be try LIMITE times |
... | ... | |
209 | 212 |
{ |
210 | 213 |
index =(i+random)%size; |
211 | 214 |
db = (DBConnection) connectionPool.elementAt(index); |
212 |
MetaCatUtil.debugMessage("Index: "+index, 65);
|
|
213 |
MetaCatUtil.debugMessage("Tag: "+db.getTag(), 65);
|
|
214 |
MetaCatUtil.debugMessage("Status: "+db.getStatus(), 65);
|
|
215 |
logMetacat.debug("Index: "+index);
|
|
216 |
logMetacat.debug("Tag: "+db.getTag());
|
|
217 |
logMetacat.debug("Status: "+db.getStatus());
|
|
215 | 218 |
//check if the connection is free |
216 | 219 |
if (db.getStatus()==FREE) |
217 | 220 |
{ |
... | ... | |
229 | 232 |
db.setCheckOutMethodName(methodName); |
230 | 233 |
db.setAutoCommit(true); |
231 | 234 |
//debug message |
232 |
MetaCatUtil.debugMessage("The connection is checked out: "
|
|
233 |
+db.getTag(), 65);
|
|
234 |
MetaCatUtil.debugMessage("The method for checking is: "
|
|
235 |
+db.getCheckOutMethodName(), 65);
|
|
236 |
MetaCatUtil.debugMessage("The age is "+db.getAge(), 65);
|
|
237 |
MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 65);
|
|
238 |
MetaCatUtil.debugMessage("The conection time it has: "
|
|
239 |
+db.getConnectionTime(), 65);
|
|
235 |
logMetacat.debug("The connection is checked out: "
|
|
236 |
+db.getTag()); |
|
237 |
logMetacat.debug("The method for checking is: "
|
|
238 |
+db.getCheckOutMethodName()); |
|
239 |
logMetacat.debug("The age is "+db.getAge());
|
|
240 |
logMetacat.debug("The usage is "+db.getUsageCount());
|
|
241 |
logMetacat.debug("The conection time it has: "
|
|
242 |
+db.getConnectionTime()); |
|
240 | 243 |
//set check out time |
241 | 244 |
db.setCheckOutTime(System.currentTimeMillis()); |
242 | 245 |
// set count of reach maximum 0 because it can check out |
... | ... | |
290 | 293 |
" open db connections is reached." + |
291 | 294 |
" New db connection to MetaCat" + |
292 | 295 |
" cannot be established.");*/ |
293 |
MetaCatUtil.debugMessage("The maximum of " +MAXIMUMCONNECTIONNUMBER +
|
|
296 |
logMetacat.fatal("The maximum of " +MAXIMUMCONNECTIONNUMBER +
|
|
294 | 297 |
" open db connections is reached." + |
295 | 298 |
" New db connection to MetaCat" + |
296 |
" cannot be established.", 1);
|
|
299 |
" cannot be established."); |
|
297 | 300 |
countOfReachMaximum ++; |
298 | 301 |
if (countOfReachMaximum >= 10) |
299 | 302 |
{ |
300 | 303 |
countOfReachMaximum =0; |
301 |
MetaCatUtil.debugMessage("finally could not get dbconnection", 1);
|
|
304 |
logMetacat.fatal("finally could not get dbconnection");
|
|
302 | 305 |
return null; |
303 | 306 |
} |
304 | 307 |
else |
... | ... | |
306 | 309 |
//if couldn't get a connection, sleep 20 seconds and try again. |
307 | 310 |
try |
308 | 311 |
{ |
309 |
MetaCatUtil.debugMessage("sleep for could not get dbconnection", 20);
|
|
312 |
logMetacat.info("sleep for could not get dbconnection");
|
|
310 | 313 |
Thread.sleep(5000); |
311 | 314 |
} |
312 | 315 |
catch (Exception e) |
313 | 316 |
{ |
317 |
logMetacat.error("Exception in getDBConnection" + e.getMessage()); |
|
314 | 318 |
} |
315 | 319 |
} |
316 | 320 |
|
... | ... | |
341 | 345 |
//Check if the DBConnection usageCount if it is too many |
342 | 346 |
if (dbConn.getUsageCount() >= MAXIMUMUSAGENUMBER ) |
343 | 347 |
{ |
344 |
MetaCatUtil.debugMessage("Connection usageCount is too many: "+
|
|
345 |
dbConn.getUsageCount(), 65);
|
|
348 |
logMetacat.debug("Connection usageCount is too many: "+
|
|
349 |
dbConn.getUsageCount()); |
|
346 | 350 |
return false; |
347 | 351 |
} |
348 | 352 |
|
349 | 353 |
//Check if the DBConnection has too much connection time |
350 | 354 |
if (dbConn.getConnectionTime() >= MAXIMUMCONNECTIONTIME) |
351 | 355 |
{ |
352 |
MetaCatUtil.debugMessage("Connection has too much connection time: "+
|
|
353 |
dbConn.getConnectionTime(), 65);
|
|
356 |
logMetacat.debug("Connection has too much connection time: "+
|
|
357 |
dbConn.getConnectionTime()); |
|
354 | 358 |
return false; |
355 | 359 |
} |
356 | 360 |
|
357 | 361 |
//Check if the DBConnection is too old |
358 | 362 |
if (dbConn.getAge() >=MAXIMUMAGE) |
359 | 363 |
{ |
360 |
MetaCatUtil.debugMessage("Connection is too old: "+dbConn.getAge(), 65);
|
|
364 |
logMetacat.debug("Connection is too old: "+dbConn.getAge());
|
|
361 | 365 |
return false; |
362 | 366 |
} |
363 | 367 |
|
... | ... | |
375 | 379 |
} |
376 | 380 |
catch (Exception e) |
377 | 381 |
{ |
378 |
MetaCatUtil.debugMessage("Error in validateDBConnection: "
|
|
379 |
+e.getMessage(), 30);
|
|
382 |
logMetacat.error("Error in validateDBConnection: "
|
|
383 |
+e.getMessage()); |
|
380 | 384 |
return false; |
381 | 385 |
} |
382 | 386 |
|
... | ... | |
397 | 401 |
index = getIndexOfPoolForConnection(conn); |
398 | 402 |
if ( index ==-1 ) |
399 | 403 |
{ |
400 |
MetaCatUtil.debugMessage("Couldn't find a DBConnection in the pool"
|
|
404 |
logMetacat.info("Couldn't find a DBConnection in the pool"
|
|
401 | 405 |
+" which have same tag to the returned" |
402 |
+" DBConnetion object", 30);
|
|
406 |
+" DBConnetion object"); |
|
403 | 407 |
return; |
404 | 408 |
|
405 | 409 |
}//if |
... | ... | |
409 | 413 |
//if it is as same as the object's checkoutserial number. |
410 | 414 |
//if it is same return it. If it is not same, maybe the connection already |
411 | 415 |
// was returned ealier. |
412 |
MetaCatUtil.debugMessage("serial number in Connection: "
|
|
413 |
+conn.getCheckOutSerialNumber(), 65);
|
|
414 |
MetaCatUtil.debugMessage("serial number in local: "+serialNumber, 65);
|
|
416 |
logMetacat.debug("serial number in Connection: "
|
|
417 |
+conn.getCheckOutSerialNumber()); |
|
418 |
logMetacat.debug("serial number in local: "+serialNumber);
|
|
415 | 419 |
if (conn.getCheckOutSerialNumber() == serialNumber) |
416 | 420 |
{ |
417 | 421 |
dbConn = (DBConnection) connectionPool.elementAt(index); |
... | ... | |
424 | 428 |
//set check out time to 0 |
425 | 429 |
dbConn.setCheckOutTime(0); |
426 | 430 |
|
427 |
MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+" checked in.", |
|
428 |
65); |
|
429 |
MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+"'s status: " |
|
430 |
+dbConn.getStatus(), 65); |
|
431 |
logMetacat.debug("Connection: "+dbConn.getTag()+" checked in."); |
|
432 |
logMetacat.debug("Connection: "+dbConn.getTag()+"'s status: " |
|
433 |
+dbConn.getStatus()); |
|
431 | 434 |
|
432 | 435 |
}//if |
433 | 436 |
else |
434 | 437 |
{ |
435 |
MetaCatUtil.debugMessage("This DBConnection couldn't return", 30);
|
|
438 |
logMetacat.info("This DBConnection couldn't return");
|
|
436 | 439 |
}//else |
437 | 440 |
}//else |
438 | 441 |
|
... | ... | |
498 | 501 |
}//try |
499 | 502 |
catch (SQLException e) |
500 | 503 |
{ |
501 |
MetaCatUtil.debugMessage("Error in release connection: "
|
|
502 |
+e.getMessage(), 10);
|
|
504 |
logMetacat.error("Error in release connection: "
|
|
505 |
+e.getMessage()); |
|
503 | 506 |
}//catch |
504 | 507 |
}//for |
505 | 508 |
}//synchronized |
... | ... | |
526 | 529 |
if (dbConn.getStatus()==BUSY && |
527 | 530 |
(System.currentTimeMillis()-dbConn.getCheckOutTime())>=30000) |
528 | 531 |
{ |
529 |
MetaCatUtil.debugMessage("This DBConnection is checked out for: "
|
|
532 |
logMetacat.fatal("This DBConnection is checked out for: "
|
|
530 | 533 |
+(System.currentTimeMillis()-dbConn.getCheckOutTime())/1000 |
531 |
+" secs" , 1);
|
|
532 |
MetaCatUtil.debugMessage(dbConn.getTag(), 1);
|
|
533 |
MetaCatUtil.debugMessage("method: "
|
|
534 |
+dbConn.getCheckOutMethodName(), 10);
|
|
534 |
+" secs"); |
|
535 |
logMetacat.fatal(dbConn.getTag());
|
|
536 |
logMetacat.error("method: "
|
|
537 |
+dbConn.getCheckOutMethodName()); |
|
535 | 538 |
|
536 | 539 |
} |
537 | 540 |
|
... | ... | |
543 | 546 |
//try to print out the warning message for every connection |
544 | 547 |
if (dbConn.getWarningMessage()!=null) |
545 | 548 |
{ |
546 |
MetaCatUtil.debugMessage("Warning for connection "
|
|
547 |
+dbConn.getTag()+" : "+ dbConn.getWarningMessage(), 10);
|
|
549 |
logMetacat.warn("Warning for connection "
|
|
550 |
+dbConn.getTag()+" : "+ dbConn.getWarningMessage()); |
|
548 | 551 |
} |
549 | 552 |
//check if it is valiate, if not create new one and replace old one |
550 | 553 |
if (!validateDBConnection(dbConn)) |
551 | 554 |
{ |
552 |
MetaCatUtil.debugMessage("Recyle it: "+ dbConn.getTag(), 65);
|
|
555 |
logMetacat.debug("Recyle it: "+ dbConn.getTag());
|
|
553 | 556 |
//close this DBConnection |
554 | 557 |
dbConn.close(); |
555 | 558 |
//remove it form connection pool |
... | ... | |
561 | 564 |
}//try |
562 | 565 |
catch (SQLException e) |
563 | 566 |
{ |
564 |
MetaCatUtil.debugMessage("Error in DBConnectionPool.run: "
|
|
565 |
+e.getMessage(), 20);
|
|
567 |
logMetacat.error("Error in DBConnectionPool.run: "
|
|
568 |
+e.getMessage()); |
|
566 | 569 |
}//catch |
567 | 570 |
}//if |
568 | 571 |
}//for |
... | ... | |
574 | 577 |
} |
575 | 578 |
catch (Exception e) |
576 | 579 |
{ |
577 |
MetaCatUtil.debugMessage("Error in DBConnectionPool.run: "
|
|
578 |
+e.getMessage(), 20);
|
|
580 |
logMetacat.error("Error in DBConnectionPool.run: "
|
|
581 |
+e.getMessage()); |
|
579 | 582 |
} |
580 | 583 |
}//while |
581 | 584 |
}//run |
... | ... | |
623 | 626 |
//check the status of db. If it is free, count it |
624 | 627 |
if (db.getStatus() == BUSY) |
625 | 628 |
{ |
626 |
MetaCatUtil.debugMessage("This method having a busy DBConnection: "
|
|
627 |
+db.getCheckOutMethodName(), 30);
|
|
628 |
MetaCatUtil.debugMessage("The busy DBConnection tag is: "
|
|
629 |
+db.getTag(), 30);
|
|
629 |
logMetacat.warn("This method having a busy DBConnection: "
|
|
630 |
+db.getCheckOutMethodName()); |
|
631 |
logMetacat.warn("The busy DBConnection tag is: "
|
|
632 |
+db.getTag()); |
|
630 | 633 |
}//if |
631 | 634 |
}//for |
632 | 635 |
|
... | ... | |
648 | 651 |
DBConnection conn = null; // the dbconnection |
649 | 652 |
connectionPoolSize = connectionPool.size(); |
650 | 653 |
freeConnectionSize = getFreeDBConnectionNumber(); |
651 |
MetaCatUtil.debugMessage("Connection pool size: " +connectionPoolSize, 3);
|
|
652 |
MetaCatUtil.debugMessage("Free Connection number: "+freeConnectionSize, 3);
|
|
654 |
logMetacat.warn("Connection pool size: " +connectionPoolSize);
|
|
655 |
logMetacat.warn("Free Connection number: "+freeConnectionSize);
|
|
653 | 656 |
difference = connectionPoolSize - freeConnectionSize; |
654 | 657 |
|
655 | 658 |
//If all connections are free and connection pool size greater than |
... | ... | |
673 | 676 |
{ |
674 | 677 |
// set hadException ture |
675 | 678 |
hasException = true; |
676 |
MetaCatUtil.debugMessage("Couldn't close a DBConnection in " +
|
|
679 |
logMetacat.error("Couldn't close a DBConnection in " +
|
|
677 | 680 |
"DBConnectionPool.shrinkDBConnectionPoolSize: " + |
678 |
e.getMessage(), 30);
|
|
681 |
e.getMessage()); |
|
679 | 682 |
}//catch |
680 | 683 |
|
681 | 684 |
//remove it from pool |
... | ... | |
710 | 713 |
DBConnection conn = null; // the dbconnection |
711 | 714 |
connectionPoolSize = connectionPool.size(); |
712 | 715 |
freeConnectionSize = getFreeDBConnectionNumber(); |
713 |
MetaCatUtil.debugMessage("Connection pool size: " +connectionPoolSize, 35);
|
|
714 |
MetaCatUtil.debugMessage("Free Connection number: "+freeConnectionSize, 35);
|
|
716 |
logMetacat.warn("Connection pool size: " +connectionPoolSize);
|
|
717 |
logMetacat.warn("Free Connection number: "+freeConnectionSize);
|
|
715 | 718 |
difference = connectionPoolSize - freeConnectionSize; |
716 | 719 |
|
717 | 720 |
//If all connections are free and connection pool size greater than |
... | ... | |
736 | 739 |
catch (SQLException e) |
737 | 740 |
{ |
738 | 741 |
|
739 |
MetaCatUtil.debugMessage("Couldn't close a DBConnection in " +
|
|
742 |
logMetacat.error("Couldn't close a DBConnection in " +
|
|
740 | 743 |
"DBConnectionPool.shrinkDBConnectionPoolSize: " + |
741 |
e.getMessage(), 30);
|
|
744 |
e.getMessage()); |
|
742 | 745 |
}//catch |
743 | 746 |
|
744 | 747 |
//remove it from pool |
Also available in: Unified diff
Replacing MetaCatUtil.debugMessage or MetaCatUtil.logMetacat call with logMetacat (private Logger object) call