Search in sources :

Example 31 with PooledConnection

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

the class ManagedPoolDataSource method connectionErrorOccurred.

/**
     *
     * A fatal error has occurred and the connection cannot be used anymore.
     * A close event from such a connection should be ignored. The connection should not be reused.
     * A new connection will be created to replace the invalid connection, when the next client
     * calls getConnection().
     */
public synchronized void connectionErrorOccurred(ConnectionEvent event) {
    PooledConnection connection = (PooledConnection) event.getSource();
    connection.removeConnectionEventListener(this);
    this.connectionsInUse.remove(connection);
    this.sessionConnectionWrappers.remove(connection);
    logInfo("Fatal exception occurred on pooled connection. Connection is removed from pool: ");
    logInfo(event.getSQLException());
    closePhysically(connection, "closing invalid, removed connection.");
    //notify threads waiting for connections or for the pool to close.
    //one waiting thread can now create a new connection since the pool has space for a new connection.
    //if a thread waits for the pool to close this could be the last unclosed connection in the pool.
    this.notifyAll();
}
Also used : PooledConnection(javax.sql.PooledConnection)

Example 32 with PooledConnection

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

the class ManagedPoolDataSource method createNewConnection.

private PooledConnection createNewConnection() throws SQLException {
    PooledConnection pooledConnection;
    // I have changed "size() + 1" to "size()".  I don't know why
    // we would want to report 1 more than the actual pool size,
    // so I am assuming that this is a coding error.  (The size
    // method does return the actual size of an array).  -blaine
    logInfo("Connection created since no connections available and " + "pool has space for more connections. Pool size: " + size());
    pooledConnection = this.connectionPoolDataSource.getPooledConnection();
    pooledConnection.addConnectionEventListener(this);
    return pooledConnection;
}
Also used : PooledConnection(javax.sql.PooledConnection)

Example 33 with PooledConnection

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

the class ManagedPoolDataSource method connectionClosed.

public synchronized void connectionClosed(ConnectionEvent event) {
    PooledConnection connection = (PooledConnection) event.getSource();
    this.connectionsInUse.remove(connection);
    this.sessionConnectionWrappers.remove(connection);
    if (!this.isPoolClosed) {
        enqueue(connection);
        logInfo("Connection returned to pool.");
    } else {
        closePhysically(connection, "closing returned connection.");
        logInfo("Connection returned to pool was closed because pool is closed.");
        //notifies evt. threads waiting for connection or for the pool to close.
        this.notifyAll();
    }
}
Also used : PooledConnection(javax.sql.PooledConnection)

Example 34 with PooledConnection

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

the class ManagedPoolDataSource method reclaimAbandonedConnections.

private void reclaimAbandonedConnections() {
    long now = System.currentTimeMillis();
    long sessionTimeoutMillis = ((long) sessionTimeout) * 1000L;
    Iterator iterator = this.connectionsInUse.iterator();
    List abandonedConnections = new ArrayList();
    while (iterator.hasNext()) {
        PooledConnection connectionInUse = (PooledConnection) iterator.next();
        SessionConnectionWrapper sessionWrapper = (SessionConnectionWrapper) this.sessionConnectionWrappers.get(connectionInUse);
        if (isSessionTimedOut(now, sessionWrapper, sessionTimeoutMillis)) {
            abandonedConnections.add(sessionWrapper);
        }
    }
    //The timed out sessions are added to a list before being closed to avoid
    //ConcurrentModificationException. When the session wrapper is closed the underlying
    // connection will eventually call connectionClosed() on this class. connectionClosed()
    // will in turn try to remove the pooled connection from the connectionsInUse set. So,
    // if the sessionWrapper is closed while iterating the connectionsInUse it would result
    // in a ConcurrentModificationException.
    iterator = abandonedConnections.iterator();
    while (iterator.hasNext()) {
        SessionConnectionWrapper sessionWrapper = (SessionConnectionWrapper) iterator.next();
        closeSessionWrapper(sessionWrapper, "Error closing abandoned session connection wrapper.");
    }
    //now have a chance to get a connections. therefore we notify all waiting threads.
    if (abandonedConnections.size() > 1) {
        abandonedConnections.clear();
        this.notifyAll();
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with PooledConnection

use of javax.sql.PooledConnection in project geode by apache.

the class GemFireConnPooledDataSource method getSQLConnection.

/**
   * gets tha connection from the pool
   * 
   * @param poolC
   * @return ???
   */
protected Connection getSQLConnection(PooledConnection poolC) throws SQLException {
    Connection conn = poolC.getConnection();
    boolean val = validateConnection(conn);
    if (val)
        return conn;
    else {
        provider.returnAndExpireConnection(poolC);
        throw new SQLException(LocalizedStrings.GemFireConnPooledDataSource_GEMFIRECONNPOOLEDDATASOURCEGETCONNFROMCONNPOOLJAVASQLCONNECTION_OBTAINED_IS_INVALID.toLocalizedString());
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection)

Aggregations

PooledConnection (javax.sql.PooledConnection)40 SQLException (java.sql.SQLException)21 Connection (java.sql.Connection)14 Test (org.junit.Test)7 Context (javax.naming.Context)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 ResultSet (java.sql.ResultSet)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 Statement (java.sql.Statement)2 Iterator (java.util.Iterator)2 XAConnection (javax.sql.XAConnection)2 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 InitialContext (javax.naming.InitialContext)1 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)1