4

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.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373

2 Answers2

8

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.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
8

Tipped by Christopher's answer, I created a tool to monitor borrowed/released connections:

https://github.com/nicolas-raoul/Commons-DBCP-monitoring

It monitors Commons DBCP usage and allows one to create such graphs:

enter image description here

enter image description here

It makes it super easy to identify which threads are holding connections for a long time.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • Would you consider refactoring your solution so that the pool itself is queryable instead of writing to `stdout`? If you do that, you could donate it back to the Tomcat project and get your name in the changelog. – Christopher Schultz Jan 03 '17 at 21:20