Search in sources :

Example 26 with ConnectionEvent

use of javax.sql.ConnectionEvent in project druid by alibaba.

the class DruidDataSourceTest_getPooledConnection method test_event_error.

public void test_event_error() throws Exception {
    DruidPooledConnection conn = (DruidPooledConnection) dataSource.getPooledConnection();
    final AtomicInteger errorCount = new AtomicInteger();
    conn.addConnectionEventListener(new ConnectionEventListener() {

        @Override
        public void connectionErrorOccurred(ConnectionEvent event) {
            errorCount.incrementAndGet();
        }

        @Override
        public void connectionClosed(ConnectionEvent event) {
        }
    });
    PreparedStatement stmt = conn.prepareStatement("select ?");
    try {
        stmt.executeQuery();
    } catch (SQLException e) {
    }
    Assert.assertEquals(1, errorCount.get());
    conn.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) ConnectionEvent(javax.sql.ConnectionEvent) PreparedStatement(java.sql.PreparedStatement) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 27 with ConnectionEvent

use of javax.sql.ConnectionEvent in project druid by alibaba.

the class ConnectionTest5 method test_handleException_5.

public void test_handleException_5() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    conn.addConnectionEventListener(new ConnectionEventListener() {

        @Override
        public void connectionClosed(ConnectionEvent event) {
        }

        @Override
        public void connectionErrorOccurred(ConnectionEvent event) {
        }
    });
    conn.close();
    {
        SQLException error = null;
        try {
            conn.handleException(new RuntimeException());
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) ConnectionEvent(javax.sql.ConnectionEvent) ConnectionEventListener(javax.sql.ConnectionEventListener)

Aggregations

ConnectionEvent (javax.sql.ConnectionEvent)27 ConnectionEventListener (javax.sql.ConnectionEventListener)23 SQLException (java.sql.SQLException)10 PooledConnection (javax.sql.PooledConnection)4 XAConnection (javax.sql.XAConnection)4 Connection (java.sql.Connection)3 Iterator (java.util.Iterator)3 XAResource (javax.transaction.xa.XAResource)3 Filter (com.alibaba.druid.filter.Filter)2 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)2 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)1 PreparedStatement (java.sql.PreparedStatement)1 Savepoint (java.sql.Savepoint)1 Statement (java.sql.Statement)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Xid (javax.transaction.xa.Xid)1 PooledObject (org.apache.tomcat.dbcp.pool2.PooledObject)1 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)1