Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat: see what threads are using the database connection pool

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.

like image 842
Nicolas Raoul Avatar asked Dec 04 '22 15:12

Nicolas Raoul


2 Answers

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.

like image 81
Nicolas Raoul Avatar answered Jan 03 '23 12:01

Nicolas Raoul


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.

like image 37
Christopher Schultz Avatar answered Jan 03 '23 12:01

Christopher Schultz