diff --git a/src/main/java/com/zaxxer/hikari/pool/PoolBase.java b/src/main/java/com/zaxxer/hikari/pool/PoolBase.java index 991ab024..07ee5694 100644 --- a/src/main/java/com/zaxxer/hikari/pool/PoolBase.java +++ b/src/main/java/com/zaxxer/hikari/pool/PoolBase.java @@ -279,8 +279,14 @@ abstract class PoolBase try { final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); - final ObjectName beanConfigName = new ObjectName("com.zaxxer.hikari:type=PoolConfig (" + poolName + ")"); - final ObjectName beanPoolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + poolName + ")"); + ObjectName beanConfigName, beanPoolName; + if ("true".equals(System.getProperty("hikaricp.jmx.register2.0"))) { + beanConfigName = new ObjectName("com.zaxxer.hikari:type=PoolConfig,name=" + poolName); + beanPoolName = new ObjectName("com.zaxxer.hikari:type=Pool,name=" + poolName); + } else { + beanConfigName = new ObjectName("com.zaxxer.hikari:type=PoolConfig (" + poolName + ")"); + beanPoolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + poolName + ")"); + } if (register) { if (!mBeanServer.isRegistered(beanConfigName)) { mBeanServer.registerMBean(config, beanConfigName); diff --git a/src/test/java/com/zaxxer/hikari/pool/TestMBean.java b/src/test/java/com/zaxxer/hikari/pool/TestMBean.java index 2f0697fa..34a9c76f 100644 --- a/src/test/java/com/zaxxer/hikari/pool/TestMBean.java +++ b/src/test/java/com/zaxxer/hikari/pool/TestMBean.java @@ -96,6 +96,25 @@ public class TestMBean finally { System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs"); } + + System.setProperty("hikaricp.jmx.register2.0", "true"); + + try (HikariDataSource ds = new HikariDataSource(config)) { + + getUnsealedConfig(ds).setIdleTimeout(3000); + + TimeUnit.SECONDS.sleep(1); + + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool,name=testMBeanReporting"); + HikariPoolMXBean hikariPoolMXBean = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class); + + assertEquals(0, hikariPoolMXBean.getActiveConnections()); + assertEquals(3, hikariPoolMXBean.getIdleConnections()); + } + finally { + System.clearProperty("hikaricp.jmx.register2.0"); + } } @Test