Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of Active Connections for HikariCP

Tags:

hikaricp

I was trying to log the number of current active connections. I am using com.zaxxer.hikari.HikariJNDIFactory as my data source factory.

final Context context = new InitialContext();
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDataSource((DataSource) ((Context)context.lookup("java:comp/env")).lookup("jdbc/mydb"));
HikariPool hikariPool = new HikariPool(hikariConfig);
LOGGER.log(Level.INFO, "The count is ::" + hikariPool.getActiveConnections());

But it is throwing the following exception:

java.lang.RuntimeException: java.lang.NullPointerException
        at com.zaxxer.hikari.util.PoolUtilities.createInstance(PoolUtilities.java:105)
        at com.zaxxer.hikari.metrics.MetricsFactory.createMetricsTracker(MetricsFactory.java:34)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:131)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:99)
        at com.something.servlet.HikariConnectionCount.doGet(HikariConnectionCount.java:35)

Where HikariConnectionCount.java is the file I have written

like image 749
Vikranth Avatar asked Nov 24 '16 11:11

Vikranth


People also ask

What is the maximum number of connection pools that HikariCP handles?

Create connection pool by setting minimumIdle to 100 and maximumPoolSize to 200.

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.

Should I close Hikari connection?

You absolutely do not want to create the HikariDataSource and close it for each SQL query. You want to create a HikariDataSource at application startup, probably as a singleton, and close it only at application shutdown. When you need to execute a SQL query, you call Connection connection = ds.


1 Answers

Programatic access is documented here https://github.com/brettwooldridge/HikariCP/wiki/MBean-(JMX)-Monitoring-and-Management

like image 155
brettw Avatar answered Sep 23 '22 14:09

brettw