Search in sources :

Example 1 with PooledConnection

use of org.apache.tomcat.jdbc.pool.PooledConnection in project tomcat by apache.

the class TestSuspectTimeout method testSuspect.

@Test
public void testSuspect() 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(100);
    this.datasource.getPoolProperties().setSuspectTimeout(1);
    this.datasource.getPoolProperties().setLogAbandoned(true);
    Connection con = datasource.getConnection();
    Assert.assertEquals("Number of connections active/busy should be 1", 1, datasource.getPool().getActive());
    Thread.sleep(3000);
    PooledConnection pcon = con.unwrap(PooledConnection.class);
    Assert.assertTrue("Connection should be marked suspect", pcon.isSuspect());
    con.close();
}
Also used : PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) Connection(java.sql.Connection) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) Test(org.junit.Test)

Example 2 with PooledConnection

use of org.apache.tomcat.jdbc.pool.PooledConnection in project dal by ctripcorp.

the class DalConnection method markDiscard.

private void markDiscard(Connection conn) throws SQLException {
    PooledConnection pConn = (PooledConnection) conn.unwrap(PooledConnection.class);
    pConn.setDiscarded(true);
}
Also used : PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection)

Example 3 with PooledConnection

use of org.apache.tomcat.jdbc.pool.PooledConnection in project dal by ctripcorp.

the class ConnectionActionTest method testCleanupCloseConnection.

@Test
public void testCleanupCloseConnection() {
    SQLException e1 = new SQLException("test discard", "1234");
    e1.setNextException(new SQLException("test discard", "08006"));
    SQLException e2 = new SQLException("test discard", "1234");
    e2.setNextException(new SQLException("test discard", "08S01"));
    Exception[] el = new Exception[] { // Case 1 detect direct SQLException
    new SQLException("test discard", "08006"), // Case 2 detect embedded SQLException wrapped by DalException
    new DalException("test discard", new SQLException("test discard", "08006")), // Case 3 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", new SQLException("test discard", "08006")), // Case 4 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", e1), // Case 1 detect direct SQLException
    new SQLException("test discard", "08006"), // Case 2 detect embedded SQLException wrapped by DalException
    new DalException("test discard", new SQLException("test discard", "08006")), // Case 3 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", new SQLException("test discard", "08006")), // Case 4 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", e2) };
    for (Exception e : el) {
        try {
            TestConnectionAction test = new TestConnectionAction();
            DalConnection connHolder = getDalConnection();
            test.connHolder = connHolder;
            test.statement = test.connHolder.getConn().createStatement();
            test.rs = test.statement.executeQuery("select * from " + SqlServerTestInitializer.TABLE_NAME);
            test.rs.next();
            PooledConnection c = (PooledConnection) connHolder.getConn().unwrap(PooledConnection.class);
            connHolder.error(e);
            test.cleanup();
            assertTrue(c.isDiscarded());
            assertTrue(c.isReleased());
            assertNotNull(test);
            assertTrue(test.conn == null);
            assertTrue(test.statement == null);
            assertTrue(test.rs == null);
            assertTrue(test.connHolder == null);
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("There should be no exception here");
        }
    }
}
Also used : DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) SQLException(java.sql.SQLException) DalException(com.ctrip.platform.dal.exceptions.DalException) DalException(com.ctrip.platform.dal.exceptions.DalException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 4 with PooledConnection

use of org.apache.tomcat.jdbc.pool.PooledConnection in project dal by ctripcorp.

the class ConnectionActionTest method testCleanupCloseConnectionNegative.

@Test
public void testCleanupCloseConnectionNegative() {
    try {
        TestConnectionAction test = new TestConnectionAction();
        DalConnection connHolder = getDalConnection();
        test.connHolder = connHolder;
        test.statement = test.connHolder.getConn().createStatement();
        test.rs = test.statement.executeQuery("select * from " + SqlServerTestInitializer.TABLE_NAME);
        test.rs.next();
        PooledConnection c = (PooledConnection) connHolder.getConn().unwrap(PooledConnection.class);
        connHolder.error(new NullPointerException("0800"));
        test.cleanup();
        assertTrue(!c.isDiscarded());
        assertTrue(!c.isReleased());
        assertNotNull(test);
        assertTrue(test.conn == null);
        assertTrue(test.statement == null);
        assertTrue(test.rs == null);
        assertTrue(test.connHolder == null);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("There should be no exception here");
    }
}
Also used : DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) DalException(com.ctrip.platform.dal.exceptions.DalException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 5 with PooledConnection

use of org.apache.tomcat.jdbc.pool.PooledConnection in project tomcat by apache.

the class ResetAbandonedTimer method resetTimer.

public boolean resetTimer() {
    boolean result = false;
    JdbcInterceptor interceptor = this.getNext();
    while (interceptor != null && result == false) {
        if (interceptor instanceof ProxyConnection) {
            PooledConnection con = ((ProxyConnection) interceptor).getConnection();
            if (con != null) {
                con.setTimestamp(System.currentTimeMillis());
                result = true;
            } else {
                break;
            }
        }
        interceptor = interceptor.getNext();
    }
    return result;
}
Also used : PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) ProxyConnection(org.apache.tomcat.jdbc.pool.ProxyConnection) JdbcInterceptor(org.apache.tomcat.jdbc.pool.JdbcInterceptor)

Aggregations

PooledConnection (org.apache.tomcat.jdbc.pool.PooledConnection)5 Test (org.junit.Test)3 DalConnection (com.ctrip.platform.dal.dao.client.DalConnection)2 DalException (com.ctrip.platform.dal.exceptions.DalException)2 SQLException (java.sql.SQLException)2 Connection (java.sql.Connection)1 JdbcInterceptor (org.apache.tomcat.jdbc.pool.JdbcInterceptor)1 ProxyConnection (org.apache.tomcat.jdbc.pool.ProxyConnection)1