Search in sources :

Example 1 with ConnectionEventListener

use of javax.sql.ConnectionEventListener in project tomcat by apache.

the class PooledConnectionImpl method notifyListeners.

/**
     * sends a connectionClosed event.
     */
void notifyListeners() {
    final ConnectionEvent event = new ConnectionEvent(this);
    final Object[] listeners = eventListeners.toArray();
    for (final Object listener : listeners) {
        ((ConnectionEventListener) listener).connectionClosed(event);
    }
}
Also used : ConnectionEvent(javax.sql.ConnectionEvent) PooledObject(org.apache.tomcat.dbcp.pool2.PooledObject) DefaultPooledObject(org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 2 with ConnectionEventListener

use of javax.sql.ConnectionEventListener in project voltdb by VoltDB.

the class LifeTimeConnectionWrapper method fireCloseEvent.

protected void fireCloseEvent() {
    ConnectionEvent connectionEvent = new ConnectionEvent(this.pooledConnection);
    for (Iterator iterator = connectionListeners.iterator(); iterator.hasNext(); ) {
        ConnectionEventListener connectionListener = (ConnectionEventListener) iterator.next();
        connectionListener.connectionClosed(connectionEvent);
    }
}
Also used : ConnectionEvent(javax.sql.ConnectionEvent) Iterator(java.util.Iterator) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 3 with ConnectionEventListener

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

the class DruidDataSource method handleConnectionException.

public void handleConnectionException(DruidPooledConnection pooledConnection, Throwable t) throws SQLException {
    final DruidConnectionHolder holder = pooledConnection.getConnectionHolder();
    errorCount.incrementAndGet();
    lastError = t;
    lastErrorTimeMillis = System.currentTimeMillis();
    if (t instanceof SQLException) {
        SQLException sqlEx = (SQLException) t;
        // broadcastConnectionError
        ConnectionEvent event = new ConnectionEvent(pooledConnection, sqlEx);
        for (ConnectionEventListener eventListener : holder.getConnectionEventListeners()) {
            eventListener.connectionErrorOccurred(event);
        }
        // exceptionSorter.isExceptionFatal
        if (exceptionSorter != null && exceptionSorter.isExceptionFatal(sqlEx)) {
            if (pooledConnection.isTraceEnable()) {
                activeConnectionLock.lock();
                try {
                    if (pooledConnection.isTraceEnable()) {
                        activeConnections.remove(pooledConnection);
                        pooledConnection.setTraceEnable(false);
                    }
                } finally {
                    activeConnectionLock.unlock();
                }
            }
            boolean requireDiscard = false;
            final ReentrantLock lock = pooledConnection.lock;
            lock.lock();
            try {
                if ((!pooledConnection.isClosed()) || !pooledConnection.isDisable()) {
                    holder.setDiscard(true);
                    pooledConnection.disable(t);
                    requireDiscard = true;
                }
            } finally {
                lock.unlock();
            }
            if (requireDiscard) {
                this.discardConnection(holder.getConnection());
                holder.setDiscard(true);
            }
            LOG.error("discard connection", sqlEx);
        }
        throw sqlEx;
    } else {
        throw new SQLException("Error", t);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) SQLException(java.sql.SQLException) ConnectionEvent(javax.sql.ConnectionEvent) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 4 with ConnectionEventListener

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

the class DruidPooledConnection method syncClose.

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

Example 5 with ConnectionEventListener

use of javax.sql.ConnectionEventListener 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)

Aggregations

ConnectionEvent (javax.sql.ConnectionEvent)8 ConnectionEventListener (javax.sql.ConnectionEventListener)8 SQLException (java.sql.SQLException)3 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