Sunday, August 30, 2009

A potential connection leak detected for connection pool

i got followwing exception in Sun Java System Application Server (GlassFish)
A potential connection leak detected for connection pool . The stack trace of the thread is provided below :com.sun.enterprise.resource.AbstractResourcePool.startConnectionLeakTracing(AbstractResourcePool.java:310)com.sun.enterprise.resource.AbstractResourcePool.setResourceStateToBusy(AbstractResourcePool.java:301)com.sun.enterprise.resource.AbstractResourcePool.getResourceFromPool(AbstractResourcePool.java:778)com.sun.enterprise.resource.AbstractResourcePool.getUnenlistedResource(AbstractResourcePool.java:652)com.sun.enterprise.resource.AbstractResourcePool.internalGetResource(AbstractResourcePool.java:594)com.sun.enterprise.resource.AbstractResourcePool.getResource(AbstractResourcePool.java:443)com.sun.enterprise.resource.PoolManagerImpl.getResourceFromPool(PoolManagerImpl.java:248)com.sun.enterprise.resource.PoolManagerImpl.getResource(PoolManagerImpl.java:176)com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:327)com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:235)
i overcome this with by following changes in Connection Pool settings




1 comments:

Jim Cullison said...

If you do that you are just covering up a real problem. The problem will come back to bite you. The connection leak detection is friend, turn it back on!

You need to look at the stack trace the leak detection gave you. Your code (or someone else's) is not calling .close() on a connection it pulled from the pool in time.

For example (DON'T DO THIS, IT LEAKS):
InitialContext ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("jdbc/my_db");

Connection con = ds.getConnection(); //to fix this, put this getConnection inside the try block, seems obvious, but I found a similar problem in "production" code

try {
Statement st = con.prepareStatement("update blah...");
st.executeUpdate(); //if this fails for any reason, the connection will NOT be returned to the pool
} finally {
con.close();
}