Hikari是Spring Boot預設的資料庫連線池。有別於C3P0直接透過連接池物件取得各項狀態指標,Hikari需要透過JMX來取得。 Demo如下,採用Spring Boot集成,定時擷取連線狀態。
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()+"]"))); } }
另外,在github有提到這樣的issue:
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + hikaridatasource.getPoolName() + ")");
可能會拋錯
程式碼效果如下圖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 thlang.main" java. reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy2.getIdleConnections(Unknown Source)
at com.zte.hikariTest.HikariTest.main(HikariTest.java:32)##:32)##usedman. .InstanceNotFoundException: com.zaxxer.hikari:type=Pool (foo)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknown Source)## at com.sun.jmxAtefor. Unknown Source)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(Unknown Source)
at com.sun.jmx.mbeanserver.MXBeanProxy$GetHandler.invoke(Unknown Source). .jmx.mbeanserver.MXBeanProxy.invoke(Unknown Source)
at javax.management.MBeanServerInvocationHandler.invoke(Unknown Source)
... 2 more
##這是因為Hikari設定參數同樣支援setHikariConfig和設定檔兩種配置方式,請選擇其中一種進行配置,而不是二者一起使用。並且請配置屬性如下,否則 JMX 無法生效。hikaridatasource.setRegisterMbeans(true);
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:07.738 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait= [0] Total=[10]
以上是如何實現Hikari連線池使用SpringBoot設定JMX監控的詳細內容。更多資訊請關注PHP中文網其他相關文章!