Initial attempt at Hikari support

This commit is contained in:
Joshua Chaitin-Pollak
2014-10-20 17:21:33 -04:00
parent c3002b6db6
commit deebf16041
4 changed files with 64 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ dependencies {
exclude(group: 'commons-logging', module: 'commons-logging')
}
optional("commons-dbcp:commons-dbcp:$commonDbcpVersion")
optional("com.zaxxer:HikariCP-java6:2.1.0")
optional("org.springframework.amqp:spring-rabbit:$springAmqpVersion")
optional("org.springframework.data:spring-data-redis:$springDataRedisVersion") {
exclude(group: 'org.springframework', module: 'spring-context-support')

View File

@@ -39,6 +39,7 @@ public abstract class DataSourceCreator<SI extends RelationalServiceInfo> extend
pooledDataSourceCreators.add(new BasicDbcpPooledDataSourceCreator<SI>());
pooledDataSourceCreators.add(new TomcatDbcpPooledDataSourceCreator<SI>());
pooledDataSourceCreators.add(new TomcatHighPerformancePooledDataSourceCreator<SI>());
pooledDataSourceCreators.add(new HikariCpPooledDataSourceCreator<SI>());
}
}

View File

@@ -0,0 +1,57 @@
package org.springframework.cloud.service.relational;
import static org.springframework.cloud.service.Util.*;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.cloud.service.ServiceConnectorConfig;
import org.springframework.cloud.service.common.RelationalServiceInfo;
import com.zaxxer.hikari.HikariDataSource;
public class HikariCpPooledDataSourceCreator<SI extends RelationalServiceInfo> implements PooledDataSourceCreator<SI> {
protected static Logger logger = Logger.getLogger(PooledDataSourceCreator.class.getName());
private static final String HIKARI_CLASSNAME = "com.zaxxer.hikari.HikariDataSource";
private DataSourceConfigurer configurer = new DataSourceConfigurer();
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);
}
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;
}
}
}

View File

@@ -35,6 +35,11 @@ public class PooledDataSourceCreatorsTest {
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),