Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring DBCP2 connection pool with JMX

I read from DBCP2 documentation that this new version supports JMX monitoring for connection pool, but I couldn't find any example which shows actual usage.

I have a simple JDBC based java application which uses dbcp2 to create a connection pool which is used by a simple query service, and I want to monitor these connection via another tool like VisualVM using JMX. DBCP2's BasicDataSource has methods like setJmxName() which I don't see any usage for that, and don't know how to use it.

If someone is not familiar with JDBC, you can read about it here.

Any help on this will be appreciated. Thanks!

like image 351
Adib Souly Avatar asked Jan 25 '16 16:01

Adib Souly


1 Answers

I haven't found any documentation either, but I have figured a few things out by digging through the source code.

You just need to create a BasicDataSource or BasicManagedDataSource object, like you normally would, and then call the setJmxName() method. The DataSource will then register itself with the platform's MBean server when you call the getConnection() method and unregister itself when you call the close() method.


Setting a JMX name of "org.apache.dbcp:DataSource=mydbname" works, an example Apache uses for unit tests can be found here.

like image 178
sam.bishop Avatar answered Sep 22 '22 11:09

sam.bishop