|
|
@ -49,18 +49,20 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
public class DataSourceJmxConfigurationTests {
|
|
|
|
public class DataSourceJmxConfigurationTests {
|
|
|
|
|
|
|
|
|
|
|
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
|
|
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
|
|
|
.withPropertyValues("spring.datasource.url="
|
|
|
|
.withPropertyValues("spring.datasource.url=" + "jdbc:hsqldb:mem:test-"
|
|
|
|
+ "jdbc:hsqldb:mem:test-" + UUID.randomUUID())
|
|
|
|
+ UUID.randomUUID())
|
|
|
|
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
|
|
|
|
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
|
|
|
|
DataSourceAutoConfiguration.class));
|
|
|
|
DataSourceAutoConfiguration.class));
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void hikariAutoConfiguredCanUseRegisterMBeans() {
|
|
|
|
public void hikariAutoConfiguredCanUseRegisterMBeans() {
|
|
|
|
String poolName = UUID.randomUUID().toString();
|
|
|
|
String poolName = UUID.randomUUID().toString();
|
|
|
|
this.contextRunner.withPropertyValues(
|
|
|
|
this.contextRunner
|
|
|
|
|
|
|
|
.withPropertyValues(
|
|
|
|
"spring.datasource.type=" + HikariDataSource.class.getName(),
|
|
|
|
"spring.datasource.type=" + HikariDataSource.class.getName(),
|
|
|
|
"spring.datasource.name=" + poolName,
|
|
|
|
"spring.datasource.name=" + poolName,
|
|
|
|
"spring.datasource.hikari.register-mbeans=true").run((context) -> {
|
|
|
|
"spring.datasource.hikari.register-mbeans=true")
|
|
|
|
|
|
|
|
.run((context) -> {
|
|
|
|
assertThat(context).hasSingleBean(HikariDataSource.class);
|
|
|
|
assertThat(context).hasSingleBean(HikariDataSource.class);
|
|
|
|
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
|
|
|
|
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
|
|
|
|
.isTrue();
|
|
|
|
.isTrue();
|
|
|
@ -75,33 +77,38 @@ public class DataSourceJmxConfigurationTests {
|
|
|
|
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
|
|
|
|
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
|
|
|
|
Set<ObjectInstance> existingInstances = mBeanServer
|
|
|
|
Set<ObjectInstance> existingInstances = mBeanServer
|
|
|
|
.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null);
|
|
|
|
.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null);
|
|
|
|
this.contextRunner.withPropertyValues(
|
|
|
|
this.contextRunner
|
|
|
|
|
|
|
|
.withPropertyValues(
|
|
|
|
"spring.datasource.type=" + HikariDataSource.class.getName(),
|
|
|
|
"spring.datasource.type=" + HikariDataSource.class.getName(),
|
|
|
|
"spring.datasource.hikari.register-mbeans=true").run((context) -> {
|
|
|
|
"spring.datasource.hikari.register-mbeans=true")
|
|
|
|
|
|
|
|
.run((context) -> {
|
|
|
|
assertThat(context).hasSingleBean(HikariDataSource.class);
|
|
|
|
assertThat(context).hasSingleBean(HikariDataSource.class);
|
|
|
|
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
|
|
|
|
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
|
|
|
|
.isTrue();
|
|
|
|
.isTrue();
|
|
|
|
// We can't rely on the number of MBeans so we're checking that the pool and pool
|
|
|
|
// We can't rely on the number of MBeans so we're checking that the
|
|
|
|
|
|
|
|
// pool and pool
|
|
|
|
// config MBeans were registered
|
|
|
|
// config MBeans were registered
|
|
|
|
assertThat(mBeanServer
|
|
|
|
assertThat(mBeanServer
|
|
|
|
.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null).size())
|
|
|
|
.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null)
|
|
|
|
.isEqualTo(existingInstances.size() + 2);
|
|
|
|
.size()).isEqualTo(existingInstances.size() + 2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void hikariAutoConfiguredUsesJmsFlag() {
|
|
|
|
public void hikariAutoConfiguredUsesJmsFlag() {
|
|
|
|
String poolName = UUID.randomUUID().toString();
|
|
|
|
String poolName = UUID.randomUUID().toString();
|
|
|
|
this.contextRunner.withPropertyValues(
|
|
|
|
this.contextRunner
|
|
|
|
|
|
|
|
.withPropertyValues(
|
|
|
|
"spring.datasource.type=" + HikariDataSource.class.getName(),
|
|
|
|
"spring.datasource.type=" + HikariDataSource.class.getName(),
|
|
|
|
"spring.jmx.enabled=false", "spring.datasource.name=" + poolName,
|
|
|
|
"spring.jmx.enabled=false", "spring.datasource.name=" + poolName,
|
|
|
|
"spring.datasource.hikari.register-mbeans=true").run((context) -> {
|
|
|
|
"spring.datasource.hikari.register-mbeans=true")
|
|
|
|
|
|
|
|
.run((context) -> {
|
|
|
|
assertThat(context).hasSingleBean(HikariDataSource.class);
|
|
|
|
assertThat(context).hasSingleBean(HikariDataSource.class);
|
|
|
|
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
|
|
|
|
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
|
|
|
|
.isTrue();
|
|
|
|
.isTrue();
|
|
|
|
// Hikari can still register mBeans
|
|
|
|
// Hikari can still register mBeans
|
|
|
|
validateHikariMBeansRegistration(ManagementFactory.getPlatformMBeanServer(),
|
|
|
|
validateHikariMBeansRegistration(
|
|
|
|
poolName, true);
|
|
|
|
ManagementFactory.getPlatformMBeanServer(), poolName, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -115,8 +122,9 @@ public class DataSourceJmxConfigurationTests {
|
|
|
|
"spring.datasource.hikari.register-mbeans=true")
|
|
|
|
"spring.datasource.hikari.register-mbeans=true")
|
|
|
|
.run((context) -> {
|
|
|
|
.run((context) -> {
|
|
|
|
assertThat(context).hasSingleBean(javax.sql.DataSource.class);
|
|
|
|
assertThat(context).hasSingleBean(javax.sql.DataSource.class);
|
|
|
|
HikariDataSource hikariDataSource = context.getBean(
|
|
|
|
HikariDataSource hikariDataSource = context
|
|
|
|
javax.sql.DataSource.class).unwrap(HikariDataSource.class);
|
|
|
|
.getBean(javax.sql.DataSource.class)
|
|
|
|
|
|
|
|
.unwrap(HikariDataSource.class);
|
|
|
|
assertThat(hikariDataSource.isRegisterMbeans()).isTrue();
|
|
|
|
assertThat(hikariDataSource.isRegisterMbeans()).isTrue();
|
|
|
|
MBeanServer mBeanServer = context.getBean(MBeanServer.class);
|
|
|
|
MBeanServer mBeanServer = context.getBean(MBeanServer.class);
|
|
|
|
validateHikariMBeansRegistration(mBeanServer, poolName, true);
|
|
|
|
validateHikariMBeansRegistration(mBeanServer, poolName, true);
|
|
|
@ -136,9 +144,10 @@ public class DataSourceJmxConfigurationTests {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void tomcatDoesNotExposeMBeanPoolByDefault() {
|
|
|
|
public void tomcatDoesNotExposeMBeanPoolByDefault() {
|
|
|
|
this.contextRunner
|
|
|
|
this.contextRunner
|
|
|
|
.withPropertyValues("spring.datasource.type=" + DataSource.class.getName())
|
|
|
|
.withPropertyValues(
|
|
|
|
.run((context) ->
|
|
|
|
"spring.datasource.type=" + DataSource.class.getName())
|
|
|
|
assertThat(context).doesNotHaveBean(ConnectionPool.class));
|
|
|
|
.run((context) -> assertThat(context)
|
|
|
|
|
|
|
|
.doesNotHaveBean(ConnectionPool.class));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -147,7 +156,8 @@ public class DataSourceJmxConfigurationTests {
|
|
|
|
"spring.datasource.type=" + DataSource.class.getName(),
|
|
|
|
"spring.datasource.type=" + DataSource.class.getName(),
|
|
|
|
"spring.datasource.jmx-enabled=true").run((context) -> {
|
|
|
|
"spring.datasource.jmx-enabled=true").run((context) -> {
|
|
|
|
assertThat(context).hasSingleBean(ConnectionPool.class);
|
|
|
|
assertThat(context).hasSingleBean(ConnectionPool.class);
|
|
|
|
assertThat(context.getBean(DataSourceProxy.class).createPool().getJmxPool())
|
|
|
|
assertThat(context.getBean(DataSourceProxy.class).createPool()
|
|
|
|
|
|
|
|
.getJmxPool())
|
|
|
|
.isSameAs(context.getBean(ConnectionPool.class));
|
|
|
|
.isSameAs(context.getBean(ConnectionPool.class));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -162,7 +172,6 @@ public class DataSourceJmxConfigurationTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class DataSourceBeanPostProcessor implements BeanPostProcessor {
|
|
|
|
private static class DataSourceBeanPostProcessor implements BeanPostProcessor {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|