use of java.sql.Connection in project tomcat by apache.
the class SimplePOJOExample method main.
public static void main(String[] args) throws Exception {
PoolConfiguration p = new PoolProperties();
p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true");
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername("root");
p.setPassword("password");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
Connection con = null;
try {
con = datasource.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from user");
int cnt = 1;
while (rs.next()) {
System.out.println((cnt++) + ". Host:" + rs.getString("Host") + " User:" + rs.getString("User") + " Password:" + rs.getString("Password"));
}
rs.close();
st.close();
} finally {
if (con != null) {
try {
con.close();
} catch (Exception ignore) {
// Ignore
}
}
}
}
use of java.sql.Connection in project tomcat by apache.
the class StarvationTest method testConnectionStarvation.
// @Test
// public void testDBCPConnectionStarvation() throws Exception {
// config();
// this.transferProperties();
// this.tDatasource.getConnection().close();
// javax.sql.DataSource datasource = this.tDatasource;
// Connection con1 = datasource.getConnection();
// Connection con2 = null;
// try {
// con2 = datasource.getConnection();
// try {
// con2.setCatalog("mysql");//make sure connection is valid
// }catch (SQLException x) {
// assertFalse("2nd Connection is not valid:"+x.getMessage(),true);
// }
// assertTrue("Connection 1 should be closed.",con1.isClosed()); //first connection should be closed
// }catch (Exception x) {
// assertFalse("Connection got starved:"+x.getMessage(),true);
// }finally {
// if (con2!=null) con2.close();
// }
// }
@Test
public void testConnectionStarvation() throws Exception {
config();
Connection con1 = datasource.getConnection();
Connection con2 = null;
try {
con2 = datasource.getConnection();
try {
//make sure connection is valid
con2.setCatalog("mysql");
} catch (SQLException x) {
Assert.assertFalse("2nd Connection is not valid:" + x.getMessage(), true);
}
//first connection should be closed
Assert.assertTrue("Connection 1 should be closed.", con1.isClosed());
} catch (Exception x) {
Assert.assertFalse("Connection got starved:" + x.getMessage(), true);
} finally {
if (con2 != null)
con2.close();
}
}
use of java.sql.Connection in project tomcat by apache.
the class TestConcurrency method testBrutalNonFair.
@Test
public void testBrutalNonFair() throws Exception {
ds.getPoolProperties().setRemoveAbandoned(false);
ds.getPoolProperties().setRemoveAbandonedTimeout(1);
ds.getPoolProperties().setMinEvictableIdleTimeMillis(100);
ds.getPoolProperties().setTimeBetweenEvictionRunsMillis(10);
ds.getConnection().close();
final int iter = 100000 * 10;
final AtomicInteger loopcount = new AtomicInteger(0);
final Runnable run = new Runnable() {
@Override
public void run() {
try {
while (loopcount.incrementAndGet() < iter) {
Connection con = ds.getConnection();
con.close();
}
} catch (Exception x) {
//stops the test
loopcount.set(iter);
x.printStackTrace();
}
}
};
Thread[] threads = new Thread[20];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(run);
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
try {
while (loopcount.get() < iter) {
Assert.assertTrue("Size comparison(less than 11):", ds.getPool().getSize() <= 10);
ds.getPool().testAllIdle();
ds.getPool().checkAbandoned();
ds.getPool().checkIdle();
}
} catch (Exception x) {
//stops the test
loopcount.set(iter);
x.printStackTrace();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
System.out.println("Connect count:" + Driver.connectCount.get());
System.out.println("DisConnect count:" + Driver.disconnectCount.get());
Assert.assertEquals("Size comparison:", 10, ds.getPool().getSize());
Assert.assertEquals("Idle comparison:", 10, ds.getPool().getIdle());
Assert.assertEquals("Used comparison:", 0, ds.getPool().getActive());
Assert.assertEquals("Connect count", 10, Driver.connectCount.get());
}
use of java.sql.Connection in project tomcat by apache.
the class AbandonPercentageTest method testDefaultAbandon.
@Test
public void testDefaultAbandon() throws Exception {
this.datasource.setMaxActive(100);
this.datasource.setMaxIdle(100);
this.datasource.setInitialSize(0);
this.datasource.getPoolProperties().setAbandonWhenPercentageFull(0);
this.datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(100);
this.datasource.getPoolProperties().setRemoveAbandoned(true);
this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
Connection con = datasource.getConnection();
Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
Thread.sleep(2000);
Assert.assertEquals("Number of connections active/busy should be 0", 0, datasource.getPool().getActive());
con.close();
}
use of java.sql.Connection in project tomcat by apache.
the class AbandonPercentageTest method testMaxedOutAbandon.
@Test
public void testMaxedOutAbandon() throws Exception {
int size = 100;
this.datasource.setMaxActive(size);
this.datasource.setMaxIdle(size);
this.datasource.setInitialSize(0);
this.datasource.getPoolProperties().setAbandonWhenPercentageFull(100);
this.datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(100);
this.datasource.getPoolProperties().setRemoveAbandoned(true);
this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
Connection con = datasource.getConnection();
Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
Thread.sleep(2000);
Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
con.close();
}
Aggregations