import javax.sql.DataSource;
-import org.apache.tomcat.jdbc.pool.DataSourceFactory;
/**
* @author Filip Hanik
for (int i=0; i<threadcount; i++) {
TestThread t = new TestThread();
t.setName("tomcat-pool-"+i);
- t.d = DataSourceFactory.getDataSource(this.datasource);
+ t.d = this.datasource;
t.start();
}
latch.await();
tearDown();
}
+ public void testC3P0Threads10Connections10() throws Exception {
+ init();
+ this.datasource.getPoolProperties().setMaxActive(10);
+ this.threadcount = 10;
+ this.transferPropertiesToC3P0();
+ this.c3p0Datasource.getConnection().close();
+ latch = new CountDownLatch(threadcount);
+ long start = System.currentTimeMillis();
+ for (int i=0; i<threadcount; i++) {
+ TestThread t = new TestThread();
+ t.setName("tomcat-pool-"+i);
+ t.d = this.c3p0Datasource;
+ t.start();
+ }
+ latch.await();
+ long delta = System.currentTimeMillis() - start;
+ System.out.println("[testC3P0Threads10Connections10]Test complete:"+delta+" ms. Iterations:"+(threadcount*this.iterations));
+ tearDown();
+ }
+
public void testDBCPThreads20Connections10() throws Exception {
init();
this.datasource.getPoolProperties().setMaxActive(10);
for (int i=0; i<threadcount; i++) {
TestThread t = new TestThread();
t.setName("tomcat-pool-"+i);
- t.d = DataSourceFactory.getDataSource(this.datasource);
+ t.d = this.datasource;
t.start();
}
latch.await();
for (int i=0; i<threadcount; i++) {
TestThread t = new TestThread();
t.setName("tomcat-pool-"+i);
- t.d = DataSourceFactory.getDataSource(this.datasource);
+ t.d = this.datasource;
t.start();
}
latch.await();
tearDown();
}
+ public void testC3P0Threads20Connections10() throws Exception {
+ init();
+ this.datasource.getPoolProperties().setMaxActive(10);
+ this.threadcount = 20;
+ this.transferPropertiesToC3P0();
+ this.c3p0Datasource.getConnection().close();
+ latch = new CountDownLatch(threadcount);
+ long start = System.currentTimeMillis();
+ for (int i=0; i<threadcount; i++) {
+ TestThread t = new TestThread();
+ t.setName("tomcat-pool-"+i);
+ t.d = this.c3p0Datasource;
+ t.start();
+ }
+ latch.await();
+ long delta = System.currentTimeMillis() - start;
+ System.out.println("[testC3P0Threads20Connections10]Test complete:"+delta+" ms. Iterations:"+(threadcount*this.iterations));
+ tearDown();
+ }
public void testDBCPThreads10Connections10Validate() throws Exception {
init();
for (int i=0; i<threadcount; i++) {
TestThread t = new TestThread();
t.setName("tomcat-pool-validate-"+i);
- t.d = DataSourceFactory.getDataSource(this.datasource);
+ t.d = this.datasource;
t.start();
}
latch.await();
System.out.println("[testPoolThreads10Connections10Validate]Test complete:"+delta+" ms. Iterations:"+(threadcount*this.iterations));
tearDown();
}
+
+ public void testC3P0Threads10Connections10Validate() throws Exception {
+ init();
+ this.datasource.getPoolProperties().setMaxActive(10);
+ this.datasource.getPoolProperties().setValidationQuery("SELECT 1");
+ this.datasource.getPoolProperties().setTestOnBorrow(true);
+ this.threadcount = 10;
+ this.transferPropertiesToC3P0();
+ this.c3p0Datasource.getConnection().close();
+ latch = new CountDownLatch(threadcount);
+ long start = System.currentTimeMillis();
+ for (int i=0; i<threadcount; i++) {
+ TestThread t = new TestThread();
+ t.setName("tomcat-pool-validate-"+i);
+ t.d = this.c3p0Datasource;
+ t.start();
+ }
+ latch.await();
+ long delta = System.currentTimeMillis() - start;
+ System.out.println("[testC3P0Threads10Connections10Validate]Test complete:"+delta+" ms. Iterations:"+(threadcount*this.iterations));
+ tearDown();
+ }
public void testDBCPThreads20Connections10Validate() throws Exception {
init();
for (int i=0; i<threadcount; i++) {
TestThread t = new TestThread();
t.setName("tomcat-pool-validate-"+i);
- t.d = DataSourceFactory.getDataSource(this.datasource);
+ t.d = this.datasource;
t.start();
}
latch.await();
tearDown();
}
+ public void testC3P0Threads10Connections20Validate() throws Exception {
+ init();
+ this.datasource.getPoolProperties().setMaxActive(10);
+ this.datasource.getPoolProperties().setValidationQuery("SELECT 1");
+ this.datasource.getPoolProperties().setTestOnBorrow(true);
+ this.threadcount = 20;
+ this.transferPropertiesToC3P0();
+ this.c3p0Datasource.getConnection().close();
+ latch = new CountDownLatch(threadcount);
+ long start = System.currentTimeMillis();
+ for (int i=0; i<threadcount; i++) {
+ TestThread t = new TestThread();
+ t.setName("tomcat-pool-validate-"+i);
+ t.d = this.c3p0Datasource;
+ t.start();
+ }
+ latch.await();
+ long delta = System.currentTimeMillis() - start;
+ System.out.println("[testC3P0Threads10Connections20Validate]Test complete:"+delta+" ms. Iterations:"+(threadcount*this.iterations));
+ tearDown();
+ }
+
public class TestThread extends Thread {
protected DataSource d;
protected String query = null;
import java.lang.reflect.Method;
import java.util.Properties;
+import javax.sql.DataSource;
+
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
+import com.mchange.v2.c3p0.ComboPooledDataSource;
+
/**
* @author Filip Hanik
* @version 1.0
*/
public class DefaultTestCase extends TestCase {
- protected DataSourceProxy datasource;
+ protected org.apache.tomcat.jdbc.pool.DataSource datasource;
protected BasicDataSource tDatasource;
+ protected DataSource c3p0Datasource;
protected int threadcount = 10;
protected int iterations = 100000;
public DefaultTestCase(String name) {
super(name);
}
- public DataSourceProxy createDefaultDataSource() {
- DataSourceProxy datasource = null;
+ public org.apache.tomcat.jdbc.pool.DataSource createDefaultDataSource() {
+ org.apache.tomcat.jdbc.pool.DataSource datasource = null;
PoolProperties p = new DefaultProperties();
p.setJmxEnabled(false);
p.setTestWhileIdle(false);
x.printStackTrace();
}
}
+
+ protected void transferPropertiesToC3P0() throws Exception {
+ System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "WARNING");
+ //http://www.mchange.com/projects/c3p0/index.html#automaticTestTable
+ ComboPooledDataSource c3p0 = new ComboPooledDataSource();
+ c3p0.setAcquireIncrement(1);
+ c3p0.setAcquireRetryAttempts(2);
+ c3p0.setAcquireRetryDelay(datasource.getPoolProperties().getMaxWait());
+ c3p0.setCheckoutTimeout(datasource.getPoolProperties().getMaxWait());
+ c3p0.setDebugUnreturnedConnectionStackTraces(datasource.getPoolProperties().isLogAbandoned());
+ c3p0.setIdleConnectionTestPeriod(datasource.getPoolProperties().getTimeBetweenEvictionRunsMillis()/1000);
+ c3p0.setInitialPoolSize(datasource.getPoolProperties().getInitialSize());
+ c3p0.setMaxIdleTime(datasource.getPoolProperties().getMinEvictableIdleTimeMillis()/1000);
+ c3p0.setMaxIdleTimeExcessConnections(datasource.getPoolProperties().getMaxIdle());
+ c3p0.setMaxPoolSize(datasource.getPoolProperties().getMaxActive());
+ c3p0.setMinPoolSize(datasource.getPoolProperties().getMinIdle());
+ c3p0.setPassword(datasource.getPoolProperties().getPassword());
+ c3p0.setPreferredTestQuery(datasource.getPoolProperties().getValidationQuery());
+ c3p0.setTestConnectionOnCheckin(datasource.getPoolProperties().isTestOnReturn());
+ c3p0.setTestConnectionOnCheckout(datasource.getPoolProperties().isTestOnBorrow());
+ c3p0.setUnreturnedConnectionTimeout(datasource.getPoolProperties().getRemoveAbandonedTimeout());
+ c3p0.setUser(datasource.getPoolProperties().getUsername());
+ c3p0.setUsesTraditionalReflectiveProxies(true);
+ c3p0.setJdbcUrl(datasource.getPoolProperties().getUrl());
+ c3p0.setDriverClass(datasource.getPoolProperties().getDriverClassName());
+ this.c3p0Datasource = c3p0;
+ /**
+ acquireIncrement
+ acquireRetryAttempts
+ acquireRetryDelay
+ autoCommitOnClose
+ automaticTestTable
+ breakAfterAcquireFailure
+ checkoutTimeout
+ connectionCustomizerClassName
+ connectionTesterClassName
+ debugUnreturnedConnectionStackTraces
+ factoryClassLocation
+ forceIgnoreUnresolvedTransactions
+ idleConnectionTestPeriod
+ initialPoolSize
+ maxAdministrativeTaskTime
+ maxConnectionAge
+ maxIdleTime
+ maxIdleTimeExcessConnections
+ maxPoolSize
+ maxStatements
+ maxStatementsPerConnection
+ minPoolSize
+ numHelperThreads
+ overrideDefaultUser
+ overrideDefaultPassword
+ password
+ preferredTestQuery
+ propertyCycle
+ testConnectionOnCheckin
+ testConnectionOnCheckout
+ unreturnedConnectionTimeout
+ user
+ usesTraditionalReflectiveProxies
+ */
+ }
protected void tearDown() throws Exception {