use of java.sql.Connection in project tomcat by apache.
the class TestValidationQueryTimeout method testLongValidationQueryTime.
@Test
public void testLongValidationQueryTime() throws Exception {
// use our mock driver
Connection con = this.datasource.getConnection();
Statement stmt = null;
long start = 0, end = 0;
try {
stmt = con.createStatement();
// set the query timeout to 2 sec
// this keeps this test from slowing things down too much
stmt.setQueryTimeout(2);
// assert that our long query takes longer than one second to run
// this is a requirement for other tests to run properly
start = System.currentTimeMillis();
stmt.execute(longQuery);
} catch (SQLTimeoutException ex) {
} catch (SQLException x) {
fail("We should have got a timeout exception.");
} finally {
end = System.currentTimeMillis();
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
Assert.assertTrue(start != 0 && end != 0);
//we're faking it
//Assert.assertTrue((end - start) > 1000);
}
}
use of java.sql.Connection in project tomcat by apache.
the class TestValidationQueryTimeout method testValidationQueryTimeoutWithQueryTimeoutInterceptor.
@Test
public void testValidationQueryTimeoutWithQueryTimeoutInterceptor() throws Exception {
int interceptorTimeout = 30;
this.datasource.setJdbcInterceptors(QueryTimeoutInterceptor.class.getName() + "(queryTimeout=" + interceptorTimeout + ")");
// because testOnBorrow is true, this triggers the validation query
Connection con = this.datasource.getConnection();
Assert.assertTrue(isTimeoutSet);
// increase the expected timeout to 30, which is what we set for the interceptor
TIMEOUT = 30;
// now create a statement, make sure the query timeout is set by the interceptor
Statement st = con.createStatement();
Assert.assertEquals(interceptorTimeout, st.getQueryTimeout());
st.close();
st = con.prepareStatement("");
Assert.assertEquals(interceptorTimeout, st.getQueryTimeout());
st.close();
st = con.prepareCall("");
Assert.assertEquals(interceptorTimeout, st.getQueryTimeout());
st.close();
con.close();
// pull another connection and check it
TIMEOUT = 10;
isTimeoutSet = false;
this.datasource.getConnection();
Assert.assertTrue(isTimeoutSet);
}
use of java.sql.Connection in project tomcat by apache.
the class TwoDataSources method testTwoDataSources.
@Test
public void testTwoDataSources() throws Exception {
org.apache.tomcat.jdbc.pool.DataSource d1 = this.createDefaultDataSource();
org.apache.tomcat.jdbc.pool.DataSource d2 = this.createDefaultDataSource();
d1.setRemoveAbandoned(true);
d1.setRemoveAbandonedTimeout(2);
d1.setTimeBetweenEvictionRunsMillis(1000);
d2.setRemoveAbandoned(false);
Connection c1 = d1.getConnection();
Connection c2 = d2.getConnection();
Thread.sleep(5000);
try {
c1.createStatement();
Assert.assertTrue("Connection should have been abandoned.", false);
} catch (Exception x) {
Assert.assertTrue("This is correct, c1 is abandoned", true);
}
try {
c2.createStatement();
Assert.assertTrue("Connection should not have been abandoned.", true);
} catch (Exception x) {
Assert.assertTrue("Connection c2 should be working", false);
}
try {
Assert.assertTrue("Connection should have been closed.", c1.isClosed());
} catch (Exception x) {
Assert.assertTrue("This is correct, c1 is closed", true);
}
try {
Assert.assertFalse("Connection c2 should not have been closed.", c2.isClosed());
} catch (Exception x) {
Assert.assertTrue("Connection c2 should be working", false);
}
}
use of java.sql.Connection in project tomcat by apache.
the class TestSlowQueryReport method testSlowSqlJmx.
@Test
public void testSlowSqlJmx() throws Exception {
int count = 1;
Connection con = this.datasource.getConnection();
for (int i = 0; i < count; i++) {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(superSlowSql);
rs.close();
st.close();
}
Map<String, SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
Assert.assertNotNull(map);
Assert.assertEquals(1, map.size());
String key = map.keySet().iterator().next();
SlowQueryReport.QueryStats stats = map.get(key);
System.out.println("Stats:" + stats);
ClientListener listener = new ClientListener();
ConnectionPool pool = datasource.getPool();
ManagementFactory.getPlatformMBeanServer().addNotificationListener(new SlowQueryReportJmx().getObjectName(SlowQueryReportJmx.class, pool.getName()), listener, null, null);
for (int i = 0; i < count; i++) {
PreparedStatement st = con.prepareStatement(superSlowSql);
ResultSet rs = st.executeQuery();
rs.close();
st.close();
}
System.out.println("Stats:" + stats);
for (int i = 0; i < count; i++) {
CallableStatement st = con.prepareCall(superSlowSql);
ResultSet rs = st.executeQuery();
rs.close();
st.close();
}
System.out.println("Stats:" + stats);
Assert.assertEquals("Expecting to have received " + (2 * count) + " notifications.", 2 * count, listener.notificationCount.get());
con.close();
tearDown();
//make sure we actually did clean up when the pool closed
Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
use of java.sql.Connection in project tomcat by apache.
the class TestStatementCache method testPreparedStatementCache2.
@Test
public void testPreparedStatementCache2() throws Exception {
init();
config(false, false, 100);
Connection con = datasource.getConnection();
PreparedStatement ps1 = con.prepareStatement("select 1");
PreparedStatement ps2 = con.prepareStatement("select 1");
Assert.assertEquals(0, interceptor.getCacheSize().get());
ps1.close();
Assert.assertTrue(ps1.isClosed());
Assert.assertEquals(0, interceptor.getCacheSize().get());
PreparedStatement ps3 = con.prepareStatement("select 1");
Assert.assertEquals(0, interceptor.getCacheSize().get());
ps2.close();
Assert.assertTrue(ps2.isClosed());
ps3.close();
Assert.assertTrue(ps3.isClosed());
Assert.assertEquals(0, interceptor.getCacheSize().get());
}
Aggregations