Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Hikari connection Pool using SpringBoot to configure JMX Monitoring

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to implement Hikari connection pool using SpringBoot configuration JMX monitoring", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to implement Hikari connection pool using SpringBoot configuration JMX monitoring"!

Hikari is the default database connection pool for Spring Boot. Unlike C3P0, which obtains the status metrics directly through the connection pool object, Hikari needs to be obtained through JMX. The Demo is as follows, using Spring Boot integration to collect the connection status 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; iTunes +) {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, there is a mention of such issue in github:

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

It may be thrown wrong.

22 Driver class com.mysql.cj.jdbc.Driver found in Thread context class loader sun.misc.Launcher$AppClassLoader@73d16e93 06 Driver class com.mysql.cj.jdbc.Driver found in Thread context class loader sun.misc.Launcher$AppClassLoader@73d16e93 23. 231 [main] 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 the Hikari setting parameter also supports both setHikariConfig and configuration files, so choose one of them to configure instead of using both. And configure the properties as follows, otherwise JMX will not take effect.

Hikaridatasource.setRegisterMbeans (true)

The code effect is as follows

2019-03-09 02 Active= 0515 INFO com.zte.hikariTest.HikariTest.69-HikariPoolState = Active= [1] Idle= [9] Wait= [0] Total= [10]

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

2019-03-09 02 Active= 0515 INFO com.zte.hikariTest.HikariTest.69-HikariPoolState = Active= [1] Idle= [9] Wait= [0] Total= [10]

2019-03-09 02 Active= 0515 INFO com.zte.hikariTest.HikariTest.69-HikariPoolState = Active= [1] Idle= [9] Wait= [0] Total= [10]

At this point, I believe you have a deeper understanding of "how to implement Hikari connection pool using SpringBoot configuration JMX monitoring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report