• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Pool

 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 {
con.setAutoCommit(true);
}
catch(SQLException e) {
// Problem with the connection, replace it.
connections.remove(con);
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?
Thanks.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Bosun
 
bill bozeman
Ranch Hand
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
[This message has been edited by bill bozeman (edited April 30, 2001).]
 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bunson and Bill!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic