Search in sources :

Example 6 with ConnectionEvent

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

the class DruidPooledConnection method close.

@Override
public void close() throws SQLException {
    if (this.disable) {
        return;
    }
    DruidConnectionHolder holder = this.holder;
    if (holder == null) {
        if (dupCloseLogEnable) {
            LOG.error("dup close");
        }
        return;
    }
    DruidAbstractDataSource dataSource = holder.getDataSource();
    boolean isSameThread = this.getOwnerThread() == Thread.currentThread();
    if (!isSameThread) {
        dataSource.setAsyncCloseConnectionEnable(true);
    }
    if (dataSource.isAsyncCloseConnectionEnable()) {
        syncClose();
        return;
    }
    for (ConnectionEventListener listener : holder.getConnectionEventListeners()) {
        listener.connectionClosed(new ConnectionEvent(this));
    }
    List<Filter> filters = dataSource.getProxyFilters();
    if (filters.size() > 0) {
        FilterChainImpl filterChain = new FilterChainImpl(dataSource);
        filterChain.dataSource_recycle(this);
    } else {
        recycle();
    }
    this.disable = true;
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) Filter(com.alibaba.druid.filter.Filter) ConnectionEvent(javax.sql.ConnectionEvent) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 7 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 8 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)

Example 9 with ConnectionEvent

use of javax.sql.ConnectionEvent in project robovm by robovm.

the class OldConnectionEventTest method testGetSQLException.

public void testGetSQLException() {
    Impl_PooledConnection ipc = new Impl_PooledConnection();
    ConnectionEvent ce = new ConnectionEvent(ipc);
    ConnectionEvent ce2 = new ConnectionEvent(ipc, null);
    assertNull(ce.getSQLException());
    assertEquals(ce2.getSQLException(), ce.getSQLException());
    SQLException e = new SQLException();
    ConnectionEvent ce3 = new ConnectionEvent(ipc, e);
    assertNotNull(ce3.getSQLException());
    assertNotSame(ce3.getSQLException(), ce2.getSQLException());
}
Also used : SQLException(java.sql.SQLException) ConnectionEvent(javax.sql.ConnectionEvent)

Example 10 with ConnectionEvent

use of javax.sql.ConnectionEvent in project robovm by robovm.

the class OldConnectionEventTest method testConstructorConnection.

public void testConstructorConnection() {
    Impl_PooledConnection ipc = new Impl_PooledConnection();
    ConnectionEvent ce = new ConnectionEvent(ipc);
    ConnectionEvent ce2 = new ConnectionEvent(ipc, null);
    assertSame(ce2.getSource(), ce.getSource());
}
Also used : ConnectionEvent(javax.sql.ConnectionEvent)

Aggregations

ConnectionEvent (javax.sql.ConnectionEvent)12 ConnectionEventListener (javax.sql.ConnectionEventListener)10 SQLException (java.sql.SQLException)4 Filter (com.alibaba.druid.filter.Filter)2 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)2 Iterator (java.util.Iterator)2 PreparedStatement (java.sql.PreparedStatement)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 PooledObject (org.apache.tomcat.dbcp.pool2.PooledObject)1 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)1