pull/9544/merge
Stephane Nicoll 8 years ago
parent 03b43225b8
commit 78a9ace582

@ -16,14 +16,11 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import java.sql.SQLException;
import javax.sql.DataSource; import javax.sql.DataSource;
import javax.sql.XADataSource; import javax.sql.XADataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@ -112,28 +109,6 @@ public class DataSourceAutoConfiguration {
} }
@Configuration
@ConditionalOnProperty(prefix = "spring.datasource", name = "jmx-enabled")
@ConditionalOnClass(name = "org.apache.tomcat.jdbc.pool.DataSourceProxy")
@Conditional(DataSourceAutoConfiguration.DataSourceAvailableCondition.class)
@ConditionalOnMissingBean(name = "dataSourceMBean")
protected static class TomcatDataSourceJmxConfiguration {
@Bean
public Object dataSourceMBean(DataSource dataSource) {
if (dataSource instanceof DataSourceProxy) {
try {
return ((DataSourceProxy) dataSource).createPool().getJmxPool();
}
catch (SQLException ex) {
logger.warn("Cannot expose DataSource to JMX (could not connect)");
}
}
return null;
}
}
/** /**
* {@link AnyNestedCondition} that checks that either {@code spring.datasource.type} * {@link AnyNestedCondition} that checks that either {@code spring.datasource.type}
* is set or {@link PooledDataSourceAvailableCondition} applies. * is set or {@link PooledDataSourceAvailableCondition} applies.

@ -16,14 +16,22 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import java.sql.SQLException;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jmx.export.MBeanExporter; import org.springframework.jmx.export.MBeanExporter;
@ -36,6 +44,8 @@ import org.springframework.jmx.export.MBeanExporter;
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
class DataSourceJmxConfiguration { class DataSourceJmxConfiguration {
private static final Log logger = LogFactory.getLog(DataSourceJmxConfiguration.class);
@Configuration @Configuration
@ConditionalOnClass(HikariDataSource.class) @ConditionalOnClass(HikariDataSource.class)
@ConditionalOnSingleCandidate(HikariDataSource.class) @ConditionalOnSingleCandidate(HikariDataSource.class)
@ -61,4 +71,26 @@ class DataSourceJmxConfiguration {
} }
@Configuration
@ConditionalOnProperty(prefix = "spring.datasource", name = "jmx-enabled")
@ConditionalOnClass(name = "org.apache.tomcat.jdbc.pool.DataSourceProxy")
@ConditionalOnSingleCandidate(DataSource.class)
static class TomcatDataSourceJmxConfiguration {
@Bean
@ConditionalOnMissingBean(name = "dataSourceMBean")
public Object dataSourceMBean(DataSource dataSource) {
if (dataSource instanceof DataSourceProxy) {
try {
return ((DataSourceProxy) dataSource).createPool().getJmxPool();
}
catch (SQLException ex) {
logger.warn("Cannot expose DataSource to JMX (could not connect)");
}
}
return null;
}
}
} }

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
import javax.management.MBeanServer; import javax.management.MBeanServer;
@ -24,6 +25,9 @@ import javax.management.MalformedObjectNameException;
import javax.management.ObjectName; import javax.management.ObjectName;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
import org.apache.tomcat.jdbc.pool.jmx.ConnectionPool;
import org.hsqldb.jdbc.JDBCDriver; import org.hsqldb.jdbc.JDBCDriver;
import org.junit.After; import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
@ -93,7 +97,6 @@ public class DataSourceJmxConfigurationTests {
// Hikari can still register mBeans // Hikari can still register mBeans
validateHikariMBeansRegistration(ManagementFactory.getPlatformMBeanServer(), validateHikariMBeansRegistration(ManagementFactory.getPlatformMBeanServer(),
poolName, true); poolName, true);
} }
private void validateHikariMBeansRegistration(MBeanServer mBeanServer, private void validateHikariMBeansRegistration(MBeanServer mBeanServer,
@ -104,6 +107,21 @@ public class DataSourceJmxConfigurationTests {
"com.zaxxer.hikari:type=PoolConfig (" + poolName + ")"))).isEqualTo(expected); "com.zaxxer.hikari:type=PoolConfig (" + poolName + ")"))).isEqualTo(expected);
} }
@Test
public void tomcatDoesNotExposeMBeanPoolByDefault() {
load("spring.datasource.type=" + DataSource.class.getName());
assertThat(this.context.getBeansOfType(ConnectionPool.class)).isEmpty();
}
@Test
public void tomcatAutoConfiguredCanExposeMBeanPool() throws SQLException {
load("spring.datasource.type=" + DataSource.class.getName(),
"spring.datasource.jmx-enabled=true");
assertThat(this.context.getBeansOfType(ConnectionPool.class)).hasSize(1);
assertThat(this.context.getBean(DataSourceProxy.class).createPool().getJmxPool())
.isSameAs(this.context.getBean(ConnectionPool.class));
}
private void load(String... environment) { private void load(String... environment) {
load(null, environment); load(null, environment);
} }

Loading…
Cancel
Save