wood burning stoves 2.0
The moose likes JDBC and Relational Databases and the fly likes Connection Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Connection Pool" Watch "Connection Pool" New topic

Connection Pool

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I am quite new to JDBC, and I have a code snippet that I am baffled with. It is a simple connection pool:
public Connection getConnection() throws SQLException {
Connection con = null;

Enumeration cons = connections.keys();

synchronized (connections) {
while(cons.hasMoreElements()) {
con = (Connection)cons.nextElement();

Boolean b = (Boolean)connections.get(con);
if (b == Boolean.FALSE) {
// So we found an unused connection.
// Test its integrity with a quick setAutoCommit(true) call.
// For production use, more testing should be performed,
// such as executing a simple query.
try {
catch(SQLException e) {
// Problem with the connection, replace it.
con = getNewConnection();
// Update the Hashtable to show this one's taken
connections.put(con, Boolean.TRUE);
// Return the connection
return con;

// If we get here, there were no free connections. Make one more.
// A more robust connection pool would have a maximum size limit,
// and would reclaim connections after some timeout period
con = getNewConnection();
connections.put(con, Boolean.FALSE);//HERE!
return con;

public void returnConnection(Connection returned) {
if (connections.containsKey(returned)) {
connections.put(returned, Boolean.FALSE);
I am confused about the line commented "HERE!". Shouldn't the value placed in the Hashtable connections be Boolean.True if all Connection objects in used are to be labelled true, and all Connections retuned labelled Boolean.False?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
The value should be false, because accroding to the comment, one more available is being added to the hashtable. i.e there are no more free connectione, so make one available by adding it to the table. I hope I am reading that correctly...


So much trouble in the world -- Bob Marley
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
I agree with you Cameron. If the method was recursive, so that if it couldn't find a connection, it created a new one, then called itself again to get that connection, that way other threads may use it if they exist, then I would say have it false. But you are returning the connection with "return con;" so, you are handing out a connection, but you are not setting it to TRUE (TRUE being "yes, this connection is in use") so other threads are going to think it is ok to use.
[This message has been edited by bill bozeman (edited April 30, 2001).]
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Thank you Bunson and Bill!
I agree. Here's the link: http://aspose.com/file-tools
subject: Connection Pool
It's not a secret anymore!