Apply eclipse formatting rules to 3dc932db
This commit is contained in:
parent
53c4859a6a
commit
adbded33ff
|
@ -30,8 +30,8 @@ import org.springframework.context.annotation.Bean;
|
|||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} that provides
|
||||
* metrics on dataSource usage.
|
||||
* {@link EnableAutoConfiguration Auto-configuration} that provides metrics on dataSource
|
||||
* usage.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.util.Collection;
|
|||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
@ -33,8 +34,7 @@ import org.springframework.context.ApplicationContext;
|
|||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
/**
|
||||
* A {@link PublicMetrics} implementation that provides data source usage
|
||||
* statistics.
|
||||
* A {@link PublicMetrics} implementation that provides data source usage statistics.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
|
@ -49,21 +49,23 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
|||
@Autowired
|
||||
private Collection<DataSourceMetadataProvider> dataSourceMetadataProviders;
|
||||
|
||||
private final Map<String, DataSourceMetadata> dataSourceMetadataByPrefix
|
||||
= new HashMap<String, DataSourceMetadata>();
|
||||
private final Map<String, DataSourceMetadata> dataSourceMetadataByPrefix = new HashMap<String, DataSourceMetadata>();
|
||||
|
||||
@PostConstruct
|
||||
public void initialize() {
|
||||
Map<String, DataSource> dataSources = this.applicationContext.getBeansOfType(DataSource.class);
|
||||
Map<String, DataSource> dataSources = this.applicationContext
|
||||
.getBeansOfType(DataSource.class);
|
||||
DataSource primaryDataSource = getPrimaryDataSource();
|
||||
|
||||
|
||||
DataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(this.dataSourceMetadataProviders);
|
||||
DataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(
|
||||
this.dataSourceMetadataProviders);
|
||||
for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
|
||||
String prefix = createPrefix(entry.getKey(), entry.getValue(), entry.getValue().equals(primaryDataSource));
|
||||
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(entry.getValue());
|
||||
String prefix = createPrefix(entry.getKey(), entry.getValue(), entry
|
||||
.getValue().equals(primaryDataSource));
|
||||
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(entry
|
||||
.getValue());
|
||||
if (dataSourceMetadata != null) {
|
||||
dataSourceMetadataByPrefix.put(prefix, dataSourceMetadata);
|
||||
this.dataSourceMetadataByPrefix.put(prefix, dataSourceMetadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -71,7 +73,8 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
|||
@Override
|
||||
public Collection<Metric<?>> metrics() {
|
||||
Collection<Metric<?>> result = new LinkedHashSet<Metric<?>>();
|
||||
for (Map.Entry<String, DataSourceMetadata> entry : dataSourceMetadataByPrefix.entrySet()) {
|
||||
for (Map.Entry<String, DataSourceMetadata> entry : this.dataSourceMetadataByPrefix
|
||||
.entrySet()) {
|
||||
String prefix = entry.getKey();
|
||||
// Make sure the prefix ends with a dot
|
||||
if (!prefix.endsWith(".")) {
|
||||
|
@ -91,19 +94,23 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create the prefix to use for the metrics to associate with the given {@link DataSource}.
|
||||
* Create the prefix to use for the metrics to associate with the given
|
||||
* {@link DataSource}.
|
||||
* @param dataSourceName the name of the data source bean
|
||||
* @param dataSource the data source to configure
|
||||
* @param primary if this data source is the primary data source
|
||||
* @return a prefix for the given data source
|
||||
*/
|
||||
protected String createPrefix(String dataSourceName, DataSource dataSource, boolean primary) {
|
||||
protected String createPrefix(String dataSourceName, DataSource dataSource,
|
||||
boolean primary) {
|
||||
StringBuilder sb = new StringBuilder("datasource.");
|
||||
if (primary) {
|
||||
sb.append("primary");
|
||||
}
|
||||
else if (endWithDataSource(dataSourceName)) { // Strip the data source part out of the name
|
||||
sb.append(dataSourceName.substring(0, dataSourceName.length() - DATASOURCE_SUFFIX.length()));
|
||||
else if (endWithDataSource(dataSourceName)) { // Strip the data source part out of
|
||||
// the name
|
||||
sb.append(dataSourceName.substring(0, dataSourceName.length()
|
||||
- DATASOURCE_SUFFIX.length()));
|
||||
}
|
||||
else {
|
||||
sb.append(dataSourceName);
|
||||
|
@ -131,7 +138,7 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
|||
*/
|
||||
private DataSource getPrimaryDataSource() {
|
||||
try {
|
||||
return applicationContext.getBean(DataSource.class);
|
||||
return this.applicationContext.getBean(DataSource.class);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException e) {
|
||||
return null;
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
package org.springframework.boot.actuate.autoconfigure;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
|
@ -26,11 +24,9 @@ import java.util.Map;
|
|||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics;
|
||||
import org.springframework.boot.actuate.endpoint.PublicMetrics;
|
||||
import org.springframework.boot.actuate.metrics.Metric;
|
||||
|
@ -44,8 +40,12 @@ import org.springframework.dao.DataAccessException;
|
|||
import org.springframework.jdbc.core.ConnectionCallback;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class MetricDataSourceAutoConfigurationTests {
|
||||
|
@ -78,24 +78,25 @@ public class MetricDataSourceAutoConfigurationTests {
|
|||
load(MultipleDataSourcesConfig.class);
|
||||
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
||||
Collection<Metric<?>> metrics = bean.metrics();
|
||||
assertMetrics(metrics,
|
||||
"datasource.tomcat.active", "datasource.tomcat.usage",
|
||||
assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage",
|
||||
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
||||
|
||||
// Hikari won't work unless a first connection has been retrieved
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(context.getBean("hikariDS", DataSource.class));
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.context.getBean("hikariDS",
|
||||
DataSource.class));
|
||||
jdbcTemplate.execute(new ConnectionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
|
||||
public Void doInConnection(Connection connection) throws SQLException,
|
||||
DataAccessException {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
Collection<Metric<?>> anotherMetrics = bean.metrics();
|
||||
assertMetrics(anotherMetrics,
|
||||
"datasource.tomcat.active", "datasource.tomcat.usage",
|
||||
"datasource.hikariDS.active", "datasource.hikariDS.usage",
|
||||
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
||||
assertMetrics(anotherMetrics, "datasource.tomcat.active",
|
||||
"datasource.tomcat.usage", "datasource.hikariDS.active",
|
||||
"datasource.hikariDS.usage", "datasource.commonsDbcp.active",
|
||||
"datasource.commonsDbcp.usage");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -103,19 +104,18 @@ public class MetricDataSourceAutoConfigurationTests {
|
|||
load(MultipleDataSourcesWithPrimaryConfig.class);
|
||||
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
||||
Collection<Metric<?>> metrics = bean.metrics();
|
||||
assertMetrics(metrics,
|
||||
"datasource.primary.active", "datasource.primary.usage",
|
||||
assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage",
|
||||
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customPrefix() {
|
||||
load(MultipleDataSourcesWithPrimaryConfig.class, CustomDataSourcePublicMetrics.class);
|
||||
load(MultipleDataSourcesWithPrimaryConfig.class,
|
||||
CustomDataSourcePublicMetrics.class);
|
||||
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
||||
Collection<Metric<?>> metrics = bean.metrics();
|
||||
assertMetrics(metrics,
|
||||
"ds.first.active", "ds.first.usage",
|
||||
"ds.second.active", "ds.second.usage");
|
||||
assertMetrics(metrics, "ds.first.active", "ds.first.usage", "ds.second.active",
|
||||
"ds.second.usage");
|
||||
|
||||
}
|
||||
|
||||
|
@ -138,13 +138,13 @@ public class MetricDataSourceAutoConfigurationTests {
|
|||
this.context.refresh();
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
static class MultipleDataSourcesConfig {
|
||||
|
||||
@Bean
|
||||
public DataSource tomcatDataSource() {
|
||||
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class).build();
|
||||
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -164,7 +164,8 @@ public class MetricDataSourceAutoConfigurationTests {
|
|||
@Bean
|
||||
@Primary
|
||||
public DataSource myDataSource() {
|
||||
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class).build();
|
||||
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -180,7 +181,8 @@ public class MetricDataSourceAutoConfigurationTests {
|
|||
public DataSourcePublicMetrics myDataSourcePublicMetrics() {
|
||||
return new DataSourcePublicMetrics() {
|
||||
@Override
|
||||
protected String createPrefix(String dataSourceName, DataSource dataSource, boolean primary) {
|
||||
protected String createPrefix(String dataSourceName,
|
||||
DataSource dataSource, boolean primary) {
|
||||
return (primary ? "ds.first." : "ds.second");
|
||||
}
|
||||
};
|
||||
|
@ -188,9 +190,7 @@ public class MetricDataSourceAutoConfigurationTests {
|
|||
}
|
||||
|
||||
private static DataSourceBuilder initializeBuilder() {
|
||||
return DataSourceBuilder.create()
|
||||
.driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
||||
.url("jdbc:hsqldb:mem:test")
|
||||
.username("sa");
|
||||
return DataSourceBuilder.create().driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
||||
.url("jdbc:hsqldb:mem:test").username("sa");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,8 @@ import javax.sql.DataSource;
|
|||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public abstract class AbstractDataSourceMetadata<D extends DataSource> implements DataSourceMetadata {
|
||||
public abstract class AbstractDataSourceMetadata<D extends DataSource> implements
|
||||
DataSourceMetadata {
|
||||
|
||||
private final D dataSource;
|
||||
|
||||
|
@ -55,7 +56,7 @@ public abstract class AbstractDataSourceMetadata<D extends DataSource> implement
|
|||
}
|
||||
|
||||
protected final D getDataSource() {
|
||||
return dataSource;
|
||||
return this.dataSource;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,13 +19,13 @@ package org.springframework.boot.autoconfigure.jdbc;
|
|||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
|
||||
/**
|
||||
* A {@link DataSourceMetadata} implementation for the commons dbcp
|
||||
* data source.
|
||||
* A {@link DataSourceMetadata} implementation for the commons dbcp data source.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class CommonsDbcpDataSourceMetadata extends AbstractDataSourceMetadata<BasicDataSource> {
|
||||
public class CommonsDbcpDataSourceMetadata extends
|
||||
AbstractDataSourceMetadata<BasicDataSource> {
|
||||
|
||||
public CommonsDbcpDataSourceMetadata(BasicDataSource dataSource) {
|
||||
super(dataSource);
|
||||
|
@ -50,4 +50,5 @@ public class CommonsDbcpDataSourceMetadata extends AbstractDataSourceMetadata<Ba
|
|||
public String getValidationQuery() {
|
||||
return getDataSource().getValidationQuery();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,7 +35,8 @@ public class CompositeDataSourceMetadataProvider implements DataSourceMetadataPr
|
|||
/**
|
||||
* Create an instance with an initial collection of delegates to use.
|
||||
*/
|
||||
public CompositeDataSourceMetadataProvider(Collection<DataSourceMetadataProvider> providers) {
|
||||
public CompositeDataSourceMetadataProvider(
|
||||
Collection<DataSourceMetadataProvider> providers) {
|
||||
this.providers = providers;
|
||||
}
|
||||
|
||||
|
@ -48,8 +49,9 @@ public class CompositeDataSourceMetadataProvider implements DataSourceMetadataPr
|
|||
|
||||
@Override
|
||||
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
|
||||
for (DataSourceMetadataProvider provider : providers) {
|
||||
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(dataSource);
|
||||
for (DataSourceMetadataProvider provider : this.providers) {
|
||||
DataSourceMetadata dataSourceMetadata = provider
|
||||
.getDataSourceMetadata(dataSource);
|
||||
if (dataSourceMetadata != null) {
|
||||
return dataSourceMetadata;
|
||||
}
|
||||
|
|
|
@ -19,9 +19,8 @@ package org.springframework.boot.autoconfigure.jdbc;
|
|||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* Provide various metadata regarding a {@link DataSource} that
|
||||
* are shared by most data source types but not accessible in a
|
||||
* standard manner.
|
||||
* Provide various metadata regarding a {@link DataSource} that are shared by most data
|
||||
* source types but not accessible in a standard manner.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
|
@ -29,44 +28,39 @@ import javax.sql.DataSource;
|
|||
public interface DataSourceMetadata {
|
||||
|
||||
/**
|
||||
* Return the usage of the pool as a double value between
|
||||
* 0 and 1.
|
||||
* Return the usage of the pool as a double value between 0 and 1.
|
||||
* <ul>
|
||||
* <li>1 means that the maximum number of connections
|
||||
* have been allocated</li>
|
||||
* <li>1 means that the maximum number of connections have been allocated</li>
|
||||
* <li>0 means that no connection is currently active</li>
|
||||
* <li>-1 means there is not limit to the number of connections
|
||||
* that can be allocated</li>
|
||||
* <li>-1 means there is not limit to the number of connections that can be allocated</li>
|
||||
* </ul>
|
||||
* This may also return {@code null} if the data source does
|
||||
* not provide the necessary information to compute the poll usage.
|
||||
* This may also return {@code null} if the data source does not provide the necessary
|
||||
* information to compute the poll usage.
|
||||
*/
|
||||
Float getPoolUsage();
|
||||
|
||||
/**
|
||||
* Return the current number of active connections that
|
||||
* have been allocated from the data source or {@code null}
|
||||
* if that information is not available.
|
||||
* Return the current number of active connections that have been allocated from the
|
||||
* data source or {@code null} if that information is not available.
|
||||
*/
|
||||
Integer getPoolSize();
|
||||
|
||||
/**
|
||||
* Return the maximum number of active connections that can be
|
||||
* allocated at the same time or {@code -1} if there is no
|
||||
* limit. Can also return {@code null} if that information is
|
||||
* not available.
|
||||
* Return the maximum number of active connections that can be allocated at the same
|
||||
* time or {@code -1} if there is no limit. Can also return {@code null} if that
|
||||
* information is not available.
|
||||
*/
|
||||
Integer getMaxPoolSize();
|
||||
|
||||
/**
|
||||
* Return the minimum number of idle connections in the pool
|
||||
* or {@code null} if that information is not available.
|
||||
* Return the minimum number of idle connections in the pool or {@code null} if that
|
||||
* information is not available.
|
||||
*/
|
||||
Integer getMinPoolSize();
|
||||
|
||||
/**
|
||||
* Return the query to use to validate that a connection is
|
||||
* valid or {@code null} if that information is not available.
|
||||
* Return the query to use to validate that a connection is valid or {@code null} if
|
||||
* that information is not available.
|
||||
*/
|
||||
String getValidationQuery();
|
||||
|
||||
|
|
|
@ -27,9 +27,8 @@ import javax.sql.DataSource;
|
|||
public interface DataSourceMetadataProvider {
|
||||
|
||||
/**
|
||||
* Return the {@link DataSourceMetadata} instance able to manage the
|
||||
* specified {@link DataSource} or {@code null} if the given data
|
||||
* source could not be handled.
|
||||
* Return the {@link DataSourceMetadata} instance able to manage the specified
|
||||
* {@link DataSource} or {@code null} if the given data source could not be handled.
|
||||
*/
|
||||
DataSourceMetadata getDataSourceMetadata(DataSource dataSource);
|
||||
|
||||
|
|
|
@ -18,16 +18,16 @@ package org.springframework.boot.autoconfigure.jdbc;
|
|||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
/**
|
||||
* Register the {@link DataSourceMetadataProvider} instances for the supported
|
||||
* data sources.
|
||||
* Register the {@link DataSourceMetadataProvider} instances for the supported data
|
||||
* sources.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
|
@ -45,12 +45,14 @@ public class DataSourceMetadataProvidersConfiguration {
|
|||
@Override
|
||||
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
|
||||
if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
|
||||
return new TomcatDataSourceMetadata((org.apache.tomcat.jdbc.pool.DataSource) dataSource);
|
||||
return new TomcatDataSourceMetadata(
|
||||
(org.apache.tomcat.jdbc.pool.DataSource) dataSource);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
@ -69,6 +71,7 @@ public class DataSourceMetadataProvidersConfiguration {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
@ -81,12 +84,14 @@ public class DataSourceMetadataProvidersConfiguration {
|
|||
@Override
|
||||
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
|
||||
if (dataSource instanceof BasicDataSource) {
|
||||
return new CommonsDbcpDataSourceMetadata((BasicDataSource) dataSource);
|
||||
return new CommonsDbcpDataSourceMetadata(
|
||||
(BasicDataSource) dataSource);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,21 +16,20 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import com.zaxxer.hikari.pool.HikariPool;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import com.zaxxer.hikari.pool.HikariPool;
|
||||
|
||||
/**
|
||||
* A {@link DataSourceMetadata} implementation for the hikari
|
||||
* data source.
|
||||
* A {@link DataSourceMetadata} implementation for the hikari data source.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariDataSource> {
|
||||
|
||||
public class HikariDataSourceMetadata extends
|
||||
AbstractDataSourceMetadata<HikariDataSource> {
|
||||
|
||||
private final HikariPoolProvider hikariPoolProvider;
|
||||
|
||||
|
@ -41,13 +40,14 @@ public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariD
|
|||
|
||||
@Override
|
||||
public Integer getPoolSize() {
|
||||
HikariPool hikariPool = hikariPoolProvider.getHikariPool();
|
||||
HikariPool hikariPool = this.hikariPoolProvider.getHikariPool();
|
||||
if (hikariPool != null) {
|
||||
return hikariPool.getActiveConnections();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMaxPoolSize() {
|
||||
return getDataSource().getMaximumPoolSize();
|
||||
}
|
||||
|
@ -63,9 +63,9 @@ public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariD
|
|||
}
|
||||
|
||||
/**
|
||||
* Provide the {@link HikariPool} instance managed internally by
|
||||
* the {@link HikariDataSource} as there is no other way to retrieve
|
||||
* that information except JMX access.
|
||||
* Provide the {@link HikariPool} instance managed internally by the
|
||||
* {@link HikariDataSource} as there is no other way to retrieve that information
|
||||
* except JMX access.
|
||||
*/
|
||||
private static class HikariPoolProvider {
|
||||
private final HikariDataSource dataSource;
|
||||
|
@ -78,7 +78,7 @@ public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariD
|
|||
}
|
||||
|
||||
public HikariPool getHikariPool() {
|
||||
if (!poolAvailable) {
|
||||
if (!this.poolAvailable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -21,8 +21,7 @@ import org.apache.tomcat.jdbc.pool.DataSource;
|
|||
|
||||
/**
|
||||
*
|
||||
* A {@link DataSourceMetadata} implementation for the tomcat
|
||||
* data source.
|
||||
* A {@link DataSourceMetadata} implementation for the tomcat data source.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
|
@ -52,4 +51,5 @@ public class TomcatDataSourceMetadata extends AbstractDataSourceMetadata<DataSou
|
|||
public String getValidationQuery() {
|
||||
return getDataSource().getValidationQuery();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,19 +16,17 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.ConnectionCallback;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSourceMetadata> {
|
||||
|
@ -51,10 +49,12 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
|
|||
@Test
|
||||
public void getPoolSizeNoConnection() {
|
||||
// Make sure the pool is initialized
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata()
|
||||
.getDataSource());
|
||||
jdbcTemplate.execute(new ConnectionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
|
||||
public Void doInConnection(Connection connection) throws SQLException,
|
||||
DataAccessException {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
@ -64,10 +64,12 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
|
|||
|
||||
@Test
|
||||
public void getPoolSizeOneConnection() {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata()
|
||||
.getDataSource());
|
||||
jdbcTemplate.execute(new ConnectionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
|
||||
public Void doInConnection(Connection connection) throws SQLException,
|
||||
DataAccessException {
|
||||
assertEquals(Integer.valueOf(1), getDataSourceMetadata().getPoolSize());
|
||||
assertEquals(Float.valueOf(0.5F), getDataSourceMetadata().getPoolUsage());
|
||||
return null;
|
||||
|
@ -77,15 +79,20 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
|
|||
|
||||
@Test
|
||||
public void getPoolSizeTwoConnections() {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata()
|
||||
.getDataSource());
|
||||
jdbcTemplate.execute(new ConnectionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
|
||||
public Void doInConnection(Connection connection) throws SQLException,
|
||||
DataAccessException {
|
||||
jdbcTemplate.execute(new ConnectionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
|
||||
assertEquals(Integer.valueOf(2), getDataSourceMetadata().getPoolSize());
|
||||
assertEquals(Float.valueOf(1F), getDataSourceMetadata().getPoolUsage());
|
||||
public Void doInConnection(Connection connection)
|
||||
throws SQLException, DataAccessException {
|
||||
assertEquals(Integer.valueOf(2), getDataSourceMetadata()
|
||||
.getPoolSize());
|
||||
assertEquals(Float.valueOf(1F), getDataSourceMetadata()
|
||||
.getPoolUsage());
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
@ -98,10 +105,8 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
|
|||
public abstract void getValidationQuery();
|
||||
|
||||
protected DataSourceBuilder initializeBuilder() {
|
||||
return DataSourceBuilder.create()
|
||||
.driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
||||
.url("jdbc:hsqldb:mem:test")
|
||||
.username("sa");
|
||||
return DataSourceBuilder.create().driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
||||
.url("jdbc:hsqldb:mem:test").username("sa");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,17 +16,18 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetadataTests<CommonsDbcpDataSourceMetadata> {
|
||||
public class CommonsDbcpDataSourceMetadataTests extends
|
||||
AbstractDataSourceMetadataTests<CommonsDbcpDataSourceMetadata> {
|
||||
|
||||
private CommonsDbcpDataSourceMetadata dataSourceMetadata;
|
||||
|
||||
|
@ -42,7 +43,8 @@ public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetada
|
|||
|
||||
@Test
|
||||
public void getPoolUsageWithNoCurrent() {
|
||||
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(createDataSource()) {
|
||||
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(
|
||||
createDataSource()) {
|
||||
@Override
|
||||
public Integer getPoolSize() {
|
||||
return null;
|
||||
|
@ -53,7 +55,8 @@ public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetada
|
|||
|
||||
@Test
|
||||
public void getPoolUsageWithNoMax() {
|
||||
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(createDataSource()) {
|
||||
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(
|
||||
createDataSource()) {
|
||||
@Override
|
||||
public Integer getMaxPoolSize() {
|
||||
return null;
|
||||
|
@ -72,10 +75,12 @@ public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetada
|
|||
public void getValidationQuery() {
|
||||
BasicDataSource dataSource = createDataSource();
|
||||
dataSource.setValidationQuery("SELECT FROM FOO");
|
||||
assertEquals("SELECT FROM FOO", new CommonsDbcpDataSourceMetadata(dataSource).getValidationQuery());
|
||||
assertEquals("SELECT FROM FOO",
|
||||
new CommonsDbcpDataSourceMetadata(dataSource).getValidationQuery());
|
||||
}
|
||||
|
||||
private CommonsDbcpDataSourceMetadata createDataSourceMetadata(int minSize, int maxSize) {
|
||||
private CommonsDbcpDataSourceMetadata createDataSourceMetadata(int minSize,
|
||||
int maxSize) {
|
||||
BasicDataSource dataSource = createDataSource();
|
||||
dataSource.setMinIdle(minSize);
|
||||
dataSource.setMaxActive(maxSize);
|
||||
|
|
|
@ -16,9 +16,6 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
@ -28,8 +25,11 @@ import org.junit.Test;
|
|||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CompositeDataSourceMetadataProviderTests {
|
||||
|
@ -55,30 +55,30 @@ public class CompositeDataSourceMetadataProviderTests {
|
|||
@Mock
|
||||
private DataSource unknownDataSource;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
given(firstProvider.getDataSourceMetadata(firstDataSource)).willReturn(first);
|
||||
given(firstProvider.getDataSourceMetadata(secondDataSource)).willReturn(second);
|
||||
given(this.firstProvider.getDataSourceMetadata(this.firstDataSource)).willReturn(
|
||||
this.first);
|
||||
given(this.firstProvider.getDataSourceMetadata(this.secondDataSource))
|
||||
.willReturn(this.second);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWithProviders() {
|
||||
CompositeDataSourceMetadataProvider provider =
|
||||
new CompositeDataSourceMetadataProvider(Arrays.asList(firstProvider, secondProvider));
|
||||
assertSame(first, provider.getDataSourceMetadata(firstDataSource));
|
||||
assertSame(second, provider.getDataSourceMetadata(secondDataSource));
|
||||
assertNull(provider.getDataSourceMetadata(unknownDataSource));
|
||||
CompositeDataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(
|
||||
Arrays.asList(this.firstProvider, this.secondProvider));
|
||||
assertSame(this.first, provider.getDataSourceMetadata(this.firstDataSource));
|
||||
assertSame(this.second, provider.getDataSourceMetadata(this.secondDataSource));
|
||||
assertNull(provider.getDataSourceMetadata(this.unknownDataSource));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addProvider() {
|
||||
CompositeDataSourceMetadataProvider provider =
|
||||
new CompositeDataSourceMetadataProvider();
|
||||
assertNull(provider.getDataSourceMetadata(firstDataSource));
|
||||
provider.addDataSourceMetadataProvider(firstProvider);
|
||||
assertSame(first, provider.getDataSourceMetadata(firstDataSource));
|
||||
CompositeDataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider();
|
||||
assertNull(provider.getDataSourceMetadata(this.firstDataSource));
|
||||
provider.addDataSourceMetadataProvider(this.firstProvider);
|
||||
assertSame(this.first, provider.getDataSourceMetadata(this.firstDataSource));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,15 +16,17 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.junit.Before;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class HikariDataSourceMetadataTests extends AbstractDataSourceMetadataTests<HikariDataSourceMetadata> {
|
||||
public class HikariDataSourceMetadataTests extends
|
||||
AbstractDataSourceMetadataTests<HikariDataSourceMetadata> {
|
||||
|
||||
private HikariDataSourceMetadata dataSourceMetadata;
|
||||
|
||||
|
@ -42,11 +44,13 @@ public class HikariDataSourceMetadataTests extends AbstractDataSourceMetadataTes
|
|||
public void getValidationQuery() {
|
||||
HikariDataSource dataSource = createDataSource(0, 4);
|
||||
dataSource.setConnectionTestQuery("SELECT FROM FOO");
|
||||
assertEquals("SELECT FROM FOO", new HikariDataSourceMetadata(dataSource).getValidationQuery());
|
||||
assertEquals("SELECT FROM FOO",
|
||||
new HikariDataSourceMetadata(dataSource).getValidationQuery());
|
||||
}
|
||||
|
||||
private HikariDataSource createDataSource(int minSize, int maxSize) {
|
||||
HikariDataSource dataSource = (HikariDataSource) initializeBuilder().type(HikariDataSource.class).build();
|
||||
HikariDataSource dataSource = (HikariDataSource) initializeBuilder().type(
|
||||
HikariDataSource.class).build();
|
||||
dataSource.setMinimumIdle(minSize);
|
||||
dataSource.setMaximumPoolSize(maxSize);
|
||||
return dataSource;
|
||||
|
|
|
@ -22,10 +22,10 @@ import org.junit.Before;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class TomcatDataSourceMetadataTests extends AbstractDataSourceMetadataTests<TomcatDataSourceMetadata> {
|
||||
public class TomcatDataSourceMetadataTests extends
|
||||
AbstractDataSourceMetadataTests<TomcatDataSourceMetadata> {
|
||||
|
||||
private TomcatDataSourceMetadata dataSourceMetadata;
|
||||
|
||||
|
@ -43,11 +43,13 @@ public class TomcatDataSourceMetadataTests extends AbstractDataSourceMetadataTes
|
|||
public void getValidationQuery() {
|
||||
DataSource dataSource = createDataSource(0, 4);
|
||||
dataSource.setValidationQuery("SELECT FROM FOO");
|
||||
assertEquals("SELECT FROM FOO", new TomcatDataSourceMetadata(dataSource).getValidationQuery());
|
||||
assertEquals("SELECT FROM FOO",
|
||||
new TomcatDataSourceMetadata(dataSource).getValidationQuery());
|
||||
}
|
||||
|
||||
private DataSource createDataSource(int minSize, int maxSize) {
|
||||
DataSource dataSource = (DataSource) initializeBuilder().type(DataSource.class).build();
|
||||
DataSource dataSource = (DataSource) initializeBuilder().type(DataSource.class)
|
||||
.build();
|
||||
dataSource.setMinIdle(minSize);
|
||||
dataSource.setMaxActive(maxSize);
|
||||
|
||||
|
|
Loading…
Reference in New Issue