Connection Pool LeakConnection Pool help to increase the performance by keep active connections to the database in the pool. The pool is normally managed by the Pool Manager. The Client (API) is supposed to borrow a connection from the pool, execute the sql query or updates and finally return back the connection to the pool. However if the connection object is not returned back to the pool, the pool will have one less connection than it should have. This is called a Connection Pool Leak. Eventually if this keep on happening the pool will be out of connections, this is called Pool Exhaustion.   

Pool Manager connection Leak

Normally this happens if the programmers forgets to invoke the return connection method or under some conditions the return connection method is not invoked.

Certain applications servers can detect whether there is a connection leak. For example Weblogic Server 8.x gives out warning in the logs when such scenario occurs.

<Feb 17, 2010 4:29:02 AM PST> <Warning> <JDBC> <BEA-001074> <A JDBC pool connection leak was detected. A connection leak occurs when a connection obtained from the pool was not closed explicitly by calling close() and then was disposed by the garbage collector and returned to the connection pool. The following stack trace at create shows wher e the leaked connection was created.  Stack trace at connection create:

        at weblogic.jdbc.wrapper.JTSConnection.initCopy( at weblogic.jdbc.jts.Driver.copyConnection( at weblogic.jdbc.jts.Driver.copyLocalConnection( at weblogic.jdbc.jts.Driver.connect( at weblogic.jdbc.common.internal.RmiDataSource.getConnection( .......

In order to find the leak, do a through code review. Make sure the connection is returned back to the pool. Also make sure all the relevant objects like PreparedStatments and ResultSet are closed. Have the cleanup of the all these objects in the finally block to be sure as shown below.

      try {  borrowConnection(); executeSQLQueryOrUpdate(); } catch (Exception e) { } finally { try {  rst.close(); } catch (SQLException e) { log.warn("Failed to close the Recordset" + e, e); } try {  ps.close(); } catch (SQLException e) { log.warn("Failed to close the Prepared Statement" + e, e); } try { // Return the Connection  returnConnection(); } catch (SQLException e) { log.warn("Failed to return the Connection" + e, e); } }