Apply pool size defaults to all DataSource implementations.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.springframework.cloud.service.relational;
|
||||
|
||||
import static org.springframework.cloud.service.PooledServiceConnectorConfig.*;
|
||||
import static org.springframework.cloud.service.Util.setCorrespondingProperties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -17,16 +18,19 @@ import org.springframework.cloud.service.PooledServiceConnectorConfigurer;
|
||||
*/
|
||||
public class DataSourceConfigurer extends PooledServiceConnectorConfigurer<DataSource, DataSourceConfig> {
|
||||
private MapServiceConnectionConfigurer<DataSource, MapServiceConnectorConfig> mapServiceConnectionConfigurer =
|
||||
new MapServiceConnectionConfigurer<DataSource, MapServiceConnectorConfig>();
|
||||
new MapServiceConnectionConfigurer<>();
|
||||
|
||||
@Override
|
||||
public DataSource configure(DataSource dataSource, DataSourceConfig config) {
|
||||
if (config != null) {
|
||||
configureConnection(dataSource, config);
|
||||
configureConnectionProperties(dataSource, config);
|
||||
return super.configure(dataSource, config);
|
||||
if (config == null) {
|
||||
// choose sensible values so that we set max connection pool size to what
|
||||
// free tier services on Cloud Foundry and Heroku allow
|
||||
config = new DataSourceConfig(new PoolConfig(4, 30000), null);
|
||||
}
|
||||
return dataSource;
|
||||
|
||||
configureConnection(dataSource, config);
|
||||
configureConnectionProperties(dataSource, config);
|
||||
return super.configure(dataSource, config);
|
||||
}
|
||||
|
||||
private void configureConnection(DataSource dataSource, DataSourceConfig config) {
|
||||
|
||||
@@ -6,7 +6,6 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.cloud.service.PooledServiceConnectorConfig.PoolConfig;
|
||||
import org.springframework.cloud.service.ServiceConnectorConfig;
|
||||
import org.springframework.cloud.service.common.RelationalServiceInfo;
|
||||
|
||||
@@ -34,11 +33,6 @@ public abstract class DbcpLikePooledDataSourceCreator<SI extends RelationalServi
|
||||
target.setPropertyValue("testOnBorrow", true);
|
||||
}
|
||||
|
||||
if (serviceConnectorConfig == null) {
|
||||
// choose sensible values so that we set max connection pool size to what
|
||||
// free tier services on Cloud Foundry and Heroku allow
|
||||
serviceConnectorConfig = new DataSourceConfig(new PoolConfig(4, 30000), null);
|
||||
}
|
||||
configurer.configure(basicDataSource, (DataSourceConfig)serviceConnectorConfig);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,10 @@ public class PooledDataSourceCreatorsTest {
|
||||
private static final int MIN_POOL_SIZE = 100;
|
||||
private static final int MAX_POOL_SIZE = 200;
|
||||
private static final int MAX_WAIT_TIME = 5;
|
||||
private static final int DEFAULT_MIN_POOL_SIZE = 0;
|
||||
private static final int DEFAULT_MAX_POOL_SIZE = 4;
|
||||
private static final int DEFAULT_MAX_WAIT_TIME = 30000;
|
||||
|
||||
private static final String CONNECTION_PROPERTIES_STRING = "useUnicode=true;characterEncoding=UTF-8";
|
||||
private static final Properties CONNECTION_PROPERTIES = new Properties() {{
|
||||
setProperty("useUnicode", "true");
|
||||
@@ -56,7 +60,13 @@ public class PooledDataSourceCreatorsTest {
|
||||
ConnectionConfig connectionConfig = new ConnectionConfig(CONNECTION_PROPERTIES_STRING);
|
||||
DataSourceConfig config = new DataSourceConfig(poolConfig, connectionConfig);
|
||||
DataSource ds = createMysqlDataSource(config);
|
||||
assertTomcatJdbcDataSource(ds);
|
||||
assertTomcatJdbcDataSource(ds, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationDefaultPools() throws Exception {
|
||||
DataSource ds = createMysqlDataSource(null);
|
||||
assertTomcatJdbcDataSource(ds, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,10 +90,10 @@ public class PooledDataSourceCreatorsTest {
|
||||
@Test
|
||||
public void pooledDataSourceCreationTomcatJdbc() throws Exception {
|
||||
DataSource ds = createMysqlDataSourceWithPooledName("TomcatJdbc");
|
||||
assertTomcatJdbcDataSource(ds);
|
||||
assertTomcatJdbcDataSource(ds, true);
|
||||
|
||||
ds = createMysqlDataSourceWithPooledName(TomcatJdbcPooledDataSourceCreator.class.getSimpleName());
|
||||
assertTomcatJdbcDataSource(ds);
|
||||
assertTomcatJdbcDataSource(ds, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,7 +106,7 @@ public class PooledDataSourceCreatorsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationInvalid() throws Exception {
|
||||
public void pooledDataSourceCreationInvalid() {
|
||||
DataSource ds = createMysqlDataSourceWithPooledName("Dummy");
|
||||
assertThat(ds, instanceOf(org.springframework.jdbc.datasource.SimpleDriverDataSource.class));
|
||||
}
|
||||
@@ -143,13 +153,21 @@ public class PooledDataSourceCreatorsTest {
|
||||
}
|
||||
}
|
||||
|
||||
private void assertTomcatJdbcDataSource(DataSource ds) throws ClassNotFoundException {
|
||||
private void assertTomcatJdbcDataSource(DataSource ds, boolean overrideConfig) throws ClassNotFoundException {
|
||||
assertThat(ds, instanceOf(Class.forName(TOMCAT_JDBC_DATASOURCE)));
|
||||
|
||||
assertEquals(MIN_POOL_SIZE, getIntValue(ds, "minIdle"));
|
||||
assertEquals(MAX_WAIT_TIME, getIntValue(ds, "maxWait"));
|
||||
// the results of setConnectionProperties are reflected by getDbProperties, not getConnectionProperties
|
||||
assertEquals(CONNECTION_PROPERTIES, getPropertiesValue(ds, "dbProperties"));
|
||||
if (overrideConfig) {
|
||||
assertEquals(MIN_POOL_SIZE, getIntValue(ds, "minIdle"));
|
||||
assertEquals(MAX_POOL_SIZE, getIntValue(ds, "maxActive"));
|
||||
assertEquals(MAX_WAIT_TIME, getIntValue(ds, "maxWait"));
|
||||
// the results of setConnectionProperties are reflected by getDbProperties, not getConnectionProperties
|
||||
assertEquals(CONNECTION_PROPERTIES, getPropertiesValue(ds, "dbProperties"));
|
||||
} else {
|
||||
assertEquals(DEFAULT_MIN_POOL_SIZE, getIntValue(ds, "minIdle"));
|
||||
assertEquals(DEFAULT_MAX_POOL_SIZE, getIntValue(ds, "maxActive"));
|
||||
assertEquals(DEFAULT_MAX_WAIT_TIME, getIntValue(ds, "maxWait"));
|
||||
assertEquals(Collections.emptyMap(), getPropertiesValue(ds, "dbProperties"));
|
||||
}
|
||||
}
|
||||
|
||||
private void assertHikariDataSource(DataSource ds) throws ClassNotFoundException {
|
||||
|
||||
Reference in New Issue
Block a user