Hikari connection pool uses springboot to configure JMX monitoring

Time:2021-10-16

Hikari is the default connection pool in springboot 2.0.
Unlike c3p0 directly obtaining various status indicators through connection pool objects, Hikari needs to obtain them through JMX.
The sample code is as follows. The springboot integration is used to regularly collect the connection status of the connection pool.

@Component
@Controller
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableScheduling
public class HikariTest {

    private static HikariPoolMXBean poolProxy;
    private static final Logger log = LoggerFactory.getLogger(HikariTest.class);

    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("db10$ZTE");
        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, on GitHub, several small partners put forward the same issue (I also met it), and

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

May throw the wrong

22:06:23.231 [main] DEBUG com.zaxxer.hikari.HikariConfig - Driver class com.mysql.cj.jdbc.Driver found in Thread context class loader [email protected]
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

Hikari setting parameters also support sethikariconfig and configuration file. Please select one of them for configuration instead of using them together. And please configure the following properties, otherwise JMX cannot take effect.

hikaridatasource.setRegisterMbeans(true);

The code effect is as follows

2019-03-09 02:05:03.725 INFO com.zte.hikariTest.HikariTest.67 -Hikari connection pool is not already...please wait...
2019-03-09 02:05:03.726 INFO o.a.c.http11.Http11NioProtocol.173 -Starting ProtocolHandler ["http-nio-8080"]
2019-03-09 02:05:03.760 INFO o.s.b.w.e.t.TomcatWebServer.204 -Tomcat started on port(s): 8080 (http) with context path ''
2019-03-09 02:05:03.763 INFO com.zte.hikariTest.HikariTest.59 -Started HikariTest in 3.487 seconds (JVM running for 3.98)
2019-03-09 02:05:03.775 INFO c.z.hikari.HikariDataSource.110 -HikariConnectionPool - Starting...
2019-03-09 02:05:04.439 INFO c.z.hikari.HikariDataSource.123 -HikariConnectionPool - Start completed.
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]

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]