I am working through Jason Hunter, Java Servlet Programming and I have a question about a comment made in the text about an example. The chapter is about database connectivity and example 9-8 showcases a connection pool. Here is a cut and paste of part of the example: 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; } } In the text, the author states "For deployment, you probably want something that does a better job of maintaining the quality of the pool and does more verification of integtity than a simple call to setAutoCommit()." What quality is he refering to? Why wouldn't a call to setAutoCommit() be good enough? If you have a connection to the database, then you have a connection. What can go wrong on this? I have zero real experience with database connections. On my PC, it always connects, so I don't have anything to relate these statements to. Any insight would be appreciated.
Even if you have the connections pooled in with you lot of things can go wrong during the real execution. The database server has the habbit of timming-out the connections that are idle for a long time. So its always better to check if the connection taken from the pool is open or closed. the connection interface provides isClosed() method to check this. Kaustubh.