replaced spaces with tabs
This commit is contained in:
@@ -22,18 +22,18 @@ import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
||||
*/
|
||||
public abstract class DataSourceCreator<SI extends RelationalServiceInfo> extends AbstractServiceConnectorCreator<DataSource, SI> {
|
||||
|
||||
protected static Logger logger = Logger.getLogger(DataSourceCreator.class.getName());
|
||||
protected static Logger logger = Logger.getLogger(DataSourceCreator.class.getName());
|
||||
|
||||
private String driverSystemPropKey;
|
||||
private String[] driverClasses;
|
||||
private String validationQuery;
|
||||
private String driverSystemPropKey;
|
||||
private String[] driverClasses;
|
||||
private String validationQuery;
|
||||
|
||||
private List<PooledDataSourceCreator<SI>> pooledDataSourceCreators = new ArrayList<PooledDataSourceCreator<SI>>();
|
||||
private List<PooledDataSourceCreator<SI>> pooledDataSourceCreators = new ArrayList<PooledDataSourceCreator<SI>>();
|
||||
|
||||
public DataSourceCreator(String driverSystemPropKey, String[] driverClasses, String validationQuery) {
|
||||
this.driverSystemPropKey = driverSystemPropKey;
|
||||
this.driverClasses = driverClasses;
|
||||
this.validationQuery = validationQuery;
|
||||
this.driverSystemPropKey = driverSystemPropKey;
|
||||
this.driverClasses = driverClasses;
|
||||
this.validationQuery = validationQuery;
|
||||
|
||||
if (pooledDataSourceCreators.size() == 0) {
|
||||
pooledDataSourceCreators.add(new BasicDbcpPooledDataSourceCreator<SI>());
|
||||
@@ -63,21 +63,21 @@ public abstract class DataSourceCreator<SI extends RelationalServiceInfo> extend
|
||||
}
|
||||
}
|
||||
|
||||
public String getDriverClassName(SI serviceInfo) {
|
||||
String userSpecifiedDriver = System.getProperty(driverSystemPropKey);
|
||||
public String getDriverClassName(SI serviceInfo) {
|
||||
String userSpecifiedDriver = System.getProperty(driverSystemPropKey);
|
||||
|
||||
if (userSpecifiedDriver != null && !userSpecifiedDriver.isEmpty()) {
|
||||
return userSpecifiedDriver;
|
||||
} else {
|
||||
for (String driver : driverClasses) {
|
||||
try {
|
||||
Class.forName(driver);
|
||||
return driver;
|
||||
} catch (ClassNotFoundException ex) {
|
||||
// continue...
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new CloudException("No suitable database driver found for " + serviceInfo.getId() + " service ");
|
||||
}
|
||||
if (userSpecifiedDriver != null && !userSpecifiedDriver.isEmpty()) {
|
||||
return userSpecifiedDriver;
|
||||
} else {
|
||||
for (String driver : driverClasses) {
|
||||
try {
|
||||
Class.forName(driver);
|
||||
return driver;
|
||||
} catch (ClassNotFoundException ex) {
|
||||
// continue...
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new CloudException("No suitable database driver found for " + serviceInfo.getId() + " service ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,43 +15,42 @@ import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
public class HikariCpPooledDataSourceCreator<SI extends RelationalServiceInfo> implements PooledDataSourceCreator<SI> {
|
||||
|
||||
protected static Logger logger = Logger.getLogger(PooledDataSourceCreator.class.getName());
|
||||
protected static Logger logger = Logger.getLogger(PooledDataSourceCreator.class.getName());
|
||||
|
||||
private static final String HIKARI_CLASSNAME = "com.zaxxer.hikari.HikariDataSource";
|
||||
|
||||
private DataSourceConfigurer configurer = new DataSourceConfigurer();
|
||||
private static final String HIKARI_CLASSNAME = "com.zaxxer.hikari.HikariDataSource";
|
||||
|
||||
protected void setBasicDataSourceProperties(DataSource basicDataSource, RelationalServiceInfo serviceInfo,
|
||||
ServiceConnectorConfig serviceConnectorConfig,
|
||||
String driverClassName, String validationQuery) {
|
||||
BeanWrapper target = new BeanWrapperImpl(basicDataSource);
|
||||
target.setPropertyValue("driverClassName", driverClassName);
|
||||
target.setPropertyValue("jdbcUrl", serviceInfo.getJdbcUrl());
|
||||
if (validationQuery != null) {
|
||||
target.setPropertyValue("connectionTestQuery", validationQuery);
|
||||
}
|
||||
private DataSourceConfigurer configurer = new DataSourceConfigurer();
|
||||
|
||||
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
|
||||
target.setPropertyValue("maximumPoolSize", 4);
|
||||
target.setPropertyValue("connectionTimeout", 30000);
|
||||
}
|
||||
configurer.configure(basicDataSource, (DataSourceConfig)serviceConnectorConfig);
|
||||
}
|
||||
protected void setBasicDataSourceProperties(DataSource basicDataSource, RelationalServiceInfo serviceInfo,
|
||||
ServiceConnectorConfig serviceConnectorConfig,
|
||||
String driverClassName, String validationQuery) {
|
||||
BeanWrapper target = new BeanWrapperImpl(basicDataSource);
|
||||
target.setPropertyValue("driverClassName", driverClassName);
|
||||
target.setPropertyValue("jdbcUrl", serviceInfo.getJdbcUrl());
|
||||
if (validationQuery != null) {
|
||||
target.setPropertyValue("connectionTestQuery", validationQuery);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig,
|
||||
String driverClassName, String validationQuery) {
|
||||
if (hasClass(HIKARI_CLASSNAME)) {
|
||||
logger.info("Found HikariCP on the classpath. Using it for DataSource connection pooling.");
|
||||
HikariDataSource ds = new HikariDataSource();
|
||||
setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery);
|
||||
return ds;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
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
|
||||
target.setPropertyValue("maximumPoolSize", 4);
|
||||
target.setPropertyValue("connectionTimeout", 30000);
|
||||
}
|
||||
configurer.configure(basicDataSource, (DataSourceConfig)serviceConnectorConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig,
|
||||
String driverClassName, String validationQuery) {
|
||||
if (hasClass(HIKARI_CLASSNAME)) {
|
||||
logger.info("Found HikariCP on the classpath. Using it for DataSource connection pooling.");
|
||||
HikariDataSource ds = new HikariDataSource();
|
||||
setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery);
|
||||
return ds;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public class PooledDataSourceCreatorsTest {
|
||||
|
||||
// Just to grab driver class name and validation query string
|
||||
private MysqlDataSourceCreator mysqlDataSourceCreator = new MysqlDataSourceCreator();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
@@ -22,30 +22,30 @@ public class PooledDataSourceCreatorsTest {
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationDbcp() {
|
||||
assertPooledDataSource(new BasicDbcpPooledDataSourceCreator<MysqlServiceInfo>());
|
||||
assertPooledDataSource(new BasicDbcpPooledDataSourceCreator<MysqlServiceInfo>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationTomcatDbcp() {
|
||||
assertPooledDataSource(new TomcatDbcpPooledDataSourceCreator<MysqlServiceInfo>());
|
||||
}
|
||||
@Test
|
||||
public void pooledDataSourceCreationTomcatDbcp() {
|
||||
assertPooledDataSource(new TomcatDbcpPooledDataSourceCreator<MysqlServiceInfo>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationTomcatHighPerformance() {
|
||||
assertPooledDataSource(new TomcatHighPerformancePooledDataSourceCreator<MysqlServiceInfo>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationHikariCP() {
|
||||
assertPooledDataSource(new HikariCpPooledDataSourceCreator<MysqlServiceInfo>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationTomcatHighPerformance() {
|
||||
assertPooledDataSource(new TomcatHighPerformancePooledDataSourceCreator<MysqlServiceInfo>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationHikariCP() {
|
||||
assertPooledDataSource(new HikariCpPooledDataSourceCreator<MysqlServiceInfo>());
|
||||
}
|
||||
|
||||
private void assertPooledDataSource(PooledDataSourceCreator<MysqlServiceInfo> testCreator) {
|
||||
DataSource ds = testCreator.create(mockMysqlServiceInfo, null,
|
||||
mysqlDataSourceCreator.getDriverClassName(mockMysqlServiceInfo),
|
||||
"select 1");
|
||||
DataSource ds = testCreator.create(mockMysqlServiceInfo, null,
|
||||
mysqlDataSourceCreator.getDriverClassName(mockMysqlServiceInfo),
|
||||
"select 1");
|
||||
|
||||
Assert.assertNotNull("Failed to create datasource with " + testCreator.getClass().getSimpleName(), ds);
|
||||
Assert.assertNotNull("Failed to create datasource with " + testCreator.getClass().getSimpleName(), ds);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user