Skip to content
Permalink
Browse files
DBCP-585 add configuration parameter to disable Connection MBean regi…
…stration (#179)

Co-authored-by: Kurt Eroglu <keroglu@atlassian.com>
  • Loading branch information
kurtcebe and kurtcebee committed May 2, 2022
1 parent 14d3df9 commit 1cc272210d60cd3b16c14ca6b8ad2a52b766f415
Showing 4 changed files with 47 additions and 2 deletions.
@@ -337,6 +337,8 @@ protected static void validateConnectionFactory(final PoolableConnectionFactory

private String jmxName;

private boolean registerConnectionMBean = true;

private boolean autoCommitOnReturn = true;

private boolean rollbackOnReturn = true;
@@ -628,7 +630,11 @@ protected PoolableConnectionFactory createPoolableConnectionFactory(final Connec
throws SQLException {
PoolableConnectionFactory connectionFactory = null;
try {
connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, ObjectNameWrapper.unwrap(registeredJmxObjectName));
if (registerConnectionMBean) {
connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, ObjectNameWrapper.unwrap(registeredJmxObjectName));
} else {
connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, null);
}
connectionFactory.setValidationQuery(validationQuery);
connectionFactory.setValidationQueryTimeout(validationQueryTimeoutDuration);
connectionFactory.setConnectionInitSql(connectionInitSqls);
@@ -2093,6 +2099,16 @@ public void setJmxName(final String jmxName) {
this.jmxName = jmxName;
}

/**
* Sets if connection level JMX tracking is requested for this DataSource. If true, each connection will be
* registered for tracking with JMX.
*
* @param registerConnectionMBean connection tracking requested for this DataSource.
*/
public void setRegisterConnectionMBean(final boolean registerConnectionMBean) {
this.registerConnectionMBean = registerConnectionMBean;
}

/**
* Sets the LIFO property. True means the pool behaves as a LIFO queue; false means FIFO.
*
@@ -91,6 +91,7 @@ public class BasicDataSourceFactory implements ObjectFactory {
private static final String PROP_VALIDATION_QUERY = "validationQuery";
private static final String PROP_VALIDATION_QUERY_TIMEOUT = "validationQueryTimeout";
private static final String PROP_JMX_NAME = "jmxName";
private static final String PROP_REGISTER_CONNECTION_MBEAN = "registerConnectionMBean";
private static final String PROP_CONNECTION_FACTORY_CLASS_NAME = "connectionFactoryClassName";

/**
@@ -149,7 +150,7 @@ public class BasicDataSourceFactory implements ObjectFactory {
PROP_MAX_OPEN_PREPARED_STATEMENTS, PROP_CONNECTION_PROPERTIES, PROP_MAX_CONN_LIFETIME_MILLIS,
PROP_LOG_EXPIRED_CONNECTIONS, PROP_ROLLBACK_ON_RETURN, PROP_ENABLE_AUTO_COMMIT_ON_RETURN,
PROP_DEFAULT_QUERY_TIMEOUT, PROP_FAST_FAIL_VALIDATION, PROP_DISCONNECTION_SQL_CODES, PROP_JMX_NAME,
PROP_CONNECTION_FACTORY_CLASS_NAME };
PROP_REGISTER_CONNECTION_MBEAN, PROP_CONNECTION_FACTORY_CLASS_NAME };

/**
* Obsolete properties from DBCP 1.x. with warning strings suggesting new properties. LinkedHashMap will guarantee
@@ -295,6 +296,7 @@ public static BasicDataSource createDataSource(final Properties properties) thro
acceptDurationOfMillis(properties, PROP_MAX_CONN_LIFETIME_MILLIS, dataSource::setMaxConn);
acceptBoolean(properties, PROP_LOG_EXPIRED_CONNECTIONS, dataSource::setLogExpiredConnections);
acceptString(properties, PROP_JMX_NAME, dataSource::setJmxName);
acceptBoolean(properties, PROP_REGISTER_CONNECTION_MBEAN, dataSource::setRegisterConnectionMBean);
acceptBoolean(properties, PROP_ENABLE_AUTO_COMMIT_ON_RETURN, dataSource::setAutoCommitOnReturn);
acceptBoolean(properties, PROP_ROLLBACK_ON_RETURN, dataSource::setRollbackOnReturn);
acceptDurationOfSeconds(properties, PROP_DEFAULT_QUERY_TIMEOUT, dataSource::setDefaultQueryTimeout);
@@ -506,6 +506,13 @@ the parent connection.
<a href="https://docs.oracle.com/javase/1.5.0/docs/api/javax/management/ObjectName.html">javadoc</a>).
</td>
</tr>
<tr>
<td>registerConnectionMBean</td>
<td>true</td>
<td>
Registers Connection JMX MBeans. See <a href="https://issues.apache.org/jira/browse/DBCP-585">DBCP-585</a>).
</td>
</tr>
</table>

</section>
@@ -633,6 +633,26 @@ public void testJmxDisabled() throws Exception {
assertEquals(0, mbs.queryNames(commons, null).size());
}

/**
* Test disabling MBean registration for Connection objects.
* JIRA: DBCP-585
*/
@Test
public void testConnectionMBeansDisabled() throws Exception {
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Unregister leftovers from other tests (TODO: worry about concurrent test execution)
final ObjectName commons = new ObjectName("org.apache.commons.*:*");
final Set<ObjectName> results = mbs.queryNames(commons, null);
for (final ObjectName result : results) {
mbs.unregisterMBean(result);
}
ds.setRegisterConnectionMBean(false); // Should disable Connection MBean registration
ds.getConnection(); // Trigger initialization
// No Connection MBeans shall be registered
final ObjectName connections = new ObjectName("org.apache.commons.*:connection=*,*");
assertEquals(0, mbs.queryNames(connections, null).size());
}

/**
* Tests JIRA <a href="https://issues.apache.org/jira/browse/DBCP-562">DBCP-562</a>.
* <p>

0 comments on commit 1cc2722

Please sign in to comment.