Search in sources :

Example 76 with Connection

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);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) SQLTimeoutException(java.sql.SQLTimeoutException) Test(org.junit.Test)

Example 77 with Connection

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);
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 78 with Connection

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);
    }
}
Also used : Connection(java.sql.Connection) Test(org.junit.Test)

Example 79 with Connection

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()));
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SlowQueryReportJmx(org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx) SlowQueryReport(org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 80 with Connection

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());
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)6326 PreparedStatement (java.sql.PreparedStatement)2793 ResultSet (java.sql.ResultSet)2657 Test (org.junit.Test)2455 SQLException (java.sql.SQLException)2267 Properties (java.util.Properties)1188 Statement (java.sql.Statement)1078 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)689 ArrayList (java.util.ArrayList)397 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)232 DataSource (javax.sql.DataSource)211 BaseTest (org.apache.phoenix.query.BaseTest)201 CallableStatement (java.sql.CallableStatement)192 IOException (java.io.IOException)158 Reader (java.io.Reader)144 DatabaseMetaData (java.sql.DatabaseMetaData)144 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)134 HashMap (java.util.HashMap)123 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)114 Timestamp (java.sql.Timestamp)113