Tomcat: see what threads are using the database connection pool

Nicolas Raoul picture Nicolas Raoul · Jul 9, 2012 · Viewed 7.1k times · Source

Tomcat has a database connection pool (DBCP) to make requests faster.

In my app, too many connections are being used for too long, I suspect a leak (connection not being properly closed) and I need to find out where is the leak.

QUESTION: How to find out the name of each thread which is using a connection?

Preferably live JMX MBean, but other tips are welcome too. Showing each thread's stack trace or class name would be acceptable too.

Note: I am not looking for an MBean that shows the DBCP settings. I want to see what uses each connection.

Answer

Christopher Schultz picture Christopher Schultz · Jul 9, 2012

Such a bean unfortunately does not exist.

What you can do is enable the logAbandoned setting in your DBCP configuration. Look at the documentation for Apache Commons-DBCP ( http://commons.apache.org/dbcp/configuration.html) for details: you can use all of the configuration options there in your <Resource> element in Tomcat.

logAbandoned will tell you where a Connection was checked-out of the pool that wasn't returned in a timely way. That can indicate a Connection leak, or just queries that are long-running.

UPDATE 2015-12-15

With Tomcat's tomcat-pool, you can attach interceptors such as the SlowQueryReport or, to get notifications via JMX, SlowQueryReportJmx

These tools might be able to do a better job than the more basic information you can get back from Apache Commons-DBCP.