Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get active connection on HikariDataSource

Tags:

hikaricp

I am trying to figure out how many connections are currently opened and I can't seem to find an obvious way to do that with Hikari.

HikariPool exposes that information (getActiveConnections) but I don't see an easy way to access that from HikariDataSource.

like image 842
Stephane Nicoll Avatar asked Jun 12 '14 15:06

Stephane Nicoll


People also ask

How does HikariCP connection pool work?

"HikariCP is solid high-performance JDBC connection pool. A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools may significantly reduce the overall resource usage." - You can find out more here.

How do I set up a Hikari connection pool?

Configuring Hikari With Spring Boot 1. x uses the Tomcat JDBC Connection Pool by default. As soon as we include spring-boot-starter-data-jpa into our pom. xml, we'll transitively include a dependency to the Tomcat JDBC implementation. During runtime, Spring Boot will then create a Tomcat DataSource for us to use.


2 Answers

If you are using spring boot:

new HikariDataSourcePoolMetadata(dataSource).getActive();
like image 135
Devs love ZenUML Avatar answered Oct 08 '22 06:10

Devs love ZenUML


You'll have to get it via JMX programmatic access. First, enable MBean registration through the registerMbeans property or by calling setRegisterMeans(). Then consult this page for how to perform programmatic access:

https://github.com/brettwooldridge/HikariCP/wiki/JMX-Monitoring

like image 36
brettw Avatar answered Oct 08 '22 08:10

brettw