From 0c8f43abea3c0c190653b46cc1aa24c00486ae4d Mon Sep 17 00:00:00 2001 From: Bryan Varner Date: Mon, 15 Oct 2012 15:51:49 -0400 Subject: [PATCH] Change how mBeans names are generated to include the data source name in the attributes (if it has a name) This allows you to query mbeans by stable name patterns, allowing for reliable instrumentation via JMX between VM instances. I currently use these patches in production (nearly 18 months) to grab pool telemetry with collectd's JMX probe. --- .../impl/PoolBackedDataSourceBase.beangen-xml | 1 + .../ActiveManagementCoordinator.java | 2 +- .../DynamicPooledDataSourceManagerMBean.java | 22 ++++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/codegen/com/mchange/v2/c3p0/impl/PoolBackedDataSourceBase.beangen-xml b/src/codegen/com/mchange/v2/c3p0/impl/PoolBackedDataSourceBase.beangen-xml index da21ada6..e6dede97 100644 --- a/src/codegen/com/mchange/v2/c3p0/impl/PoolBackedDataSourceBase.beangen-xml +++ b/src/codegen/com/mchange/v2/c3p0/impl/PoolBackedDataSourceBase.beangen-xml @@ -52,6 +52,7 @@ String dataSourceName + null publicsynchronized publicsynchronized diff --git a/src/java/com/mchange/v2/c3p0/management/ActiveManagementCoordinator.java b/src/java/com/mchange/v2/c3p0/management/ActiveManagementCoordinator.java index 576e68e4..08ce4ef5 100644 --- a/src/java/com/mchange/v2/c3p0/management/ActiveManagementCoordinator.java +++ b/src/java/com/mchange/v2/c3p0/management/ActiveManagementCoordinator.java @@ -146,6 +146,6 @@ public void attemptUnmanagePooledDataSource(PooledDataSource pds) } private String getPdsObjectNameStr(PooledDataSource pds) - { return "com.mchange.v2.c3p0:type=PooledDataSource[" + pds.getIdentityToken() + "]"; } + { return "com.mchange.v2.c3p0:type=PooledDataSource,identityToken=" + pds.getIdentityToken(); } } diff --git a/src/java/com/mchange/v2/c3p0/management/DynamicPooledDataSourceManagerMBean.java b/src/java/com/mchange/v2/c3p0/management/DynamicPooledDataSourceManagerMBean.java index 653f26b5..e266abef 100644 --- a/src/java/com/mchange/v2/c3p0/management/DynamicPooledDataSourceManagerMBean.java +++ b/src/java/com/mchange/v2/c3p0/management/DynamicPooledDataSourceManagerMBean.java @@ -43,6 +43,7 @@ import javax.management.MBeanParameterInfo; import javax.management.MBeanServer; import javax.management.ObjectName; +import javax.management.ObjectInstance; import javax.management.ReflectionException; import javax.sql.ConnectionPoolDataSource; import javax.sql.DataSource; @@ -120,6 +121,7 @@ public class DynamicPooledDataSourceManagerMBean implements DynamicMBean PooledDataSource pds; String mbeanName; + String oldPdsName; MBeanServer mbs; ConnectionPoolDataSource cpds; @@ -137,8 +139,14 @@ public void propertyChange(PropertyChangeEvent evt) String propName = evt.getPropertyName(); Object val = evt.getNewValue(); - if ("nestedDataSource".equals(propName) || "connectionPoolDataSource".equals(propName)) + if ("nestedDataSource".equals(propName) || "connectionPoolDataSource".equals(propName)) { reinitialize(); + } else if ("dataSourceName".equals(propName)) { + if (evt.getOldValue() != null) { + oldPdsName = evt.getOldValue().toString(); + } + reinitialize(); + } } }; @@ -147,10 +155,11 @@ public DynamicPooledDataSourceManagerMBean(PooledDataSource pds, String mbeanNam { this.pds = pds; this.mbeanName = mbeanName; + this.oldPdsName = pds.getDataSourceName(); this.mbs = mbs; if (pds instanceof ComboPooledDataSource) - /* do nothing */; + ((ComboPooledDataSource) pds).addPropertyChangeListener(pcl); else if (pds instanceof AbstractPoolBackedDataSource) ((AbstractPoolBackedDataSource) pds).addPropertyChangeListener(pcl); else @@ -224,17 +233,18 @@ private synchronized Exception reinitialize() // that the MBeanInfo is reread. try { - ObjectName oname = ObjectName.getInstance( mbeanName ); + ObjectName oname = ObjectName.getInstance( mbeanName + ",name=" + oldPdsName ); if (mbs.isRegistered( oname )) { mbs.unregisterMBean( oname ); if (logger.isLoggable(MLevel.FINER)) - logger.log(MLevel.FINER, "MBean: " + mbeanName + " unregistered, in order to be reregistered after update."); + logger.log(MLevel.FINER, "MBean: " + oname.toString() + " unregistered, in order to be reregistered after update."); } + oname = ObjectName.getInstance(mbeanName + ",name=" + pds.getDataSourceName()); mbs.registerMBean( this, oname ); if (logger.isLoggable(MLevel.FINER)) - logger.log(MLevel.FINER, "MBean: " + mbeanName + " registered."); - + logger.log(MLevel.FINER, "MBean: " + oname.toString() + " registered."); + return null; } catch (Exception e)