diff --git a/server/src/main/java/com/ning/billing/server/modules/DBIProvider.java b/server/src/main/java/com/ning/billing/server/modules/DBIProvider.java
index 5d86c2b..66c5cdf 100644
--- a/server/src/main/java/com/ning/billing/server/modules/DBIProvider.java
+++ b/server/src/main/java/com/ning/billing/server/modules/DBIProvider.java
@@ -20,6 +20,7 @@ import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
+import org.skife.config.TimeSpan;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.TimingCollector;
import org.skife.jdbi.v2.tweak.SQLLog;
@@ -65,8 +66,8 @@ public class DBIProvider implements Provider<DBI> {
@Override
public DBI get() {
+ final DataSource ds = getDataSource();
- final DataSource ds = getC3P0DataSource();
final DBI dbi = new DBI(ds);
dbi.registerArgumentFactory(new UUIDArgumentFactory());
dbi.registerArgumentFactory(new DateTimeZoneArgumentFactory());
@@ -95,6 +96,21 @@ public class DBIProvider implements Provider<DBI> {
return dbi;
}
+ private DataSource getDataSource() {
+ final DataSource ds;
+
+ // TODO PIERRE DaoConfig is in the skeleton
+ final String dataSource = System.getProperty("com.ning.jetty.jdbi.datasource", "c3p0");
+ if (dataSource.equals("c3p0")) {
+ ds = getC3P0DataSource();
+ } else if (dataSource.equals("bonecp")) {
+ ds = getBoneCPDatSource();
+ } else {
+ throw new IllegalArgumentException("DataSource " + dataSource + " unsupported");
+ }
+
+ return ds;
+ }
private DataSource getBoneCPDatSource() {
final BoneCPConfig dbConfig = new BoneCPConfig();
@@ -110,17 +126,53 @@ public class DBIProvider implements Provider<DBI> {
dbConfig.setPartitionCount(1);
dbConfig.setDisableJMX(false);
- final BoneCPDataSource ds = new BoneCPDataSource(dbConfig);
- return ds;
+ return new BoneCPDataSource(dbConfig);
}
private DataSource getC3P0DataSource() {
- ComboPooledDataSource cpds = new ComboPooledDataSource();
+ final ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setJdbcUrl(config.getJdbcUrl());
cpds.setUser(config.getUsername());
cpds.setPassword(config.getPassword());
- cpds.setMinPoolSize(1);
- cpds.setMaxPoolSize(10);
+ // http://www.mchange.com/projects/c3p0/#minPoolSize
+ // Minimum number of Connections a pool will maintain at any given time.
+ cpds.setMinPoolSize(config.getMinIdle());
+ // http://www.mchange.com/projects/c3p0/#maxPoolSize
+ // Maximum number of Connections a pool will maintain at any given time.
+ cpds.setMaxPoolSize(config.getMaxActive());
+ // http://www.mchange.com/projects/c3p0/#checkoutTimeout
+ // The number of milliseconds a client calling getConnection() will wait for a Connection to be checked-in or
+ // acquired when the pool is exhausted. Zero means wait indefinitely. Setting any positive value will cause the getConnection()
+ // call to time-out and break with an SQLException after the specified number of milliseconds.
+ cpds.setCheckoutTimeout(toMilliSeconds(config.getConnectionTimeout()));
+ // http://www.mchange.com/projects/c3p0/#maxIdleTime
+ // Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.
+ cpds.setMaxIdleTime(toSeconds(config.getIdleMaxAge()));
+ // http://www.mchange.com/projects/c3p0/#maxConnectionAge
+ // Seconds, effectively a time to live. A Connection older than maxConnectionAge will be destroyed and purged from the pool.
+ // This differs from maxIdleTime in that it refers to absolute age. Even a Connection which has not been much idle will be purged
+ // from the pool if it exceeds maxConnectionAge. Zero means no maximum absolute age is enforced.
+ cpds.setMaxConnectionAge(toSeconds(config.getMaxConnectionAge()));
+ // http://www.mchange.com/projects/c3p0/#idleConnectionTestPeriod
+ // If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds.
+ cpds.setIdleConnectionTestPeriod(toSeconds(config.getIdleConnectionTestPeriod()));
+
return cpds;
}
+
+ private int toSeconds(final TimeSpan timeSpan) {
+ return toSeconds(timeSpan.getPeriod(), timeSpan.getUnit());
+ }
+
+ private int toSeconds(final long period, final TimeUnit timeUnit) {
+ return (int) TimeUnit.SECONDS.convert(period, timeUnit);
+ }
+
+ private int toMilliSeconds(final TimeSpan timeSpan) {
+ return toMilliSeconds(timeSpan.getPeriod(), timeSpan.getUnit());
+ }
+
+ private int toMilliSeconds(final long period, final TimeUnit timeUnit) {
+ return (int) TimeUnit.MILLISECONDS.convert(period, timeUnit);
+ }
}