Hikari connection pooling uses SpringBoot to configure JMX monitoring implementation

  • 2021-10-24 22:59:16
  • OfStack

Hikari is the default database connection pool for Spring and Boot. Unlike C3P0, which directly obtains various status indicators through connection pool objects, Hikari needs to be obtained through JMX. Demo is as follows, Spring Boot integration is adopted, and connection status is collected regularly.


public static void main(String[] args) throws SQLException, MalformedObjectNameException, InterruptedException {
 SpringApplication.run(HikariTest.class, args);
 HikariDataSource hikaridatasource = new HikariDataSource();
 hikaridatasource.setJdbcUrl("jdbc:mysql://localhost:3306?serverTimezone=GMT");
 hikaridatasource.setUsername("root");
 hikaridatasource.setPassword("");
 hikaridatasource.setDriverClassName("com.mysql.cj.jdbc.Driver");
 hikaridatasource.setRegisterMbeans(true);
 hikaridatasource.setPoolName("HikariConnectionPool");
 MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
 ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + hikaridatasource.getPoolName() + ")");
 poolProxy = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
 Connection conn = hikaridatasource.getConnection();
 Statement sm = conn.createStatement();
 ResultSet rs = null;
 for (int i = 0; i < 999999999; i++) {
  rs = sm.executeQuery("select name from test.t1");
 }
 rs.close();
 sm.close();
 conn.close();
 hikaridatasource.close();
}

@Scheduled(fixedRate = 1000)
public void HikariMonitor() {
 if(poolProxy == null) {
  log.info("Hikari not initialized,please wait...");
 }else {
  log.info("HikariPoolState = "
  + "Active=[" + String.valueOf(poolProxy.getActiveConnections() + "] "
  + "Idle=[" + String.valueOf(poolProxy.getIdleConnections() + "] "
  + "Wait=["+poolProxy.getThreadsAwaitingConnection()+"] "
  + "Total=["+poolProxy.getTotalConnections()+"]")));
 } 
}

In addition, issue is mentioned in github:


ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + hikaridatasource.getPoolName() + ")");

It may be thrown incorrectly

22:06:23.231 [main] DEBUG com.zaxxer.hikari.HikariConfig - Driver class com.mysql.cj.jdbc.Driver found in Thread context class loader sun.misc.Launcher$AppClassLoader@73d16e93
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy2.getIdleConnections(Unknown Source)
at com.zte.hikariTest.HikariTest.main(HikariTest.java:32)
Caused by: javax.management.InstanceNotFoundException: com.zaxxer.hikari:type=Pool (foo)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknown Source)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(Unknown Source)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(Unknown Source)
at com.sun.jmx.mbeanserver.MXBeanProxy$GetHandler.invoke(Unknown Source)
at com.sun.jmx.mbeanserver.MXBeanProxy.invoke(Unknown Source)
at javax.management.MBeanServerInvocationHandler.invoke(Unknown Source)
... 2 more

This is because Hikari setting parameters also support setHikariConfig and configuration files. Please select one of them for configuration instead of using one of the two. And please configure the properties as follows, otherwise JMX will not take effect.


hikaridatasource.setRegisterMbeans(true);

The code effect is as follows

2019-03-09 02:05:04.738 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0] Total=[10]
2019-03-09 02:05:05.740 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0] Total=[10]
2019-03-09 02:05:06.732 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0] Total=[10]
2019-03-09 02:05:07.738 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0] Total=[10]


Related articles: