The Joy of Finding Connection Leaks

August 8th, 2005 by Mathias Meyer

Don’t you just love it? You do some stuff in your application, click here, click there, and JBoss’ JMX console keeps telling you that there are fewer and fewer available connections in the data-source’s pool.
You look into Hibernate, look at JBoss’ log output, debug the closing of Hibernate sessions (which it did as expected), have more and more doubts about JBoss’ Hibernate integration (and yourself, for that matter), try this, try that, set time-outs for idle connections, and still, connections are leaking. Entries in the JBoss forums didn’t help, any recommendation on time-outs didn’t work. But then: CachedConnectionManager to the rescue. My hint in this case is to use the JMX console and look at the CachedConnectionManager’s service page, to be found in the section jboss.jca. It has a nice method to list all connections currently in use (canonically called listInUseConnections()). It does not just list them, but provides a nice stack-trace of where the connection has been opened. Ah, the joy of connections not being closed in custom code not using Hibernate, but directly using a connection from the data-source. Additionally, you can tell JBoss to pop out a stack-trace each time it closes a statement for you. The corresponding entry in the -ds.xml looks like this:

<datasources>
  <local-tx-datasource>
    .....
    <track-statements>true</track-statements>
  </local-tx-datasource>
</datasources>

Nice way to find leaks in development (in production as well, but as the saying of JBoss goes: please do your own housekeeping).
Lesson learned here? Select isn’t broken.

Leave a Reply


ok