Search in sources :

Example 21 with PooledConnection

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

the class TestGetConnection method testGetConnection.

@Test
public void testGetConnection() throws Exception {
    Connection con = this.datasource.getConnection();
    Assert.assertTrue("Connection should implement javax.sql.PooledConnection", con instanceof PooledConnection);
    Connection actual = ((PooledConnection) con).getConnection();
    Assert.assertNotNull("Connection delegate should not be null.", actual);
    System.out.println("Actual connection:" + actual.getClass().getName());
}
Also used : PooledConnection(javax.sql.PooledConnection) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) Test(org.junit.Test)

Example 22 with PooledConnection

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

the class KeyedCPDSConnectionFactory method destroyObject.

/**
     * Closes the PooledConnection and stops listening for events from it.
     */
@Override
public void destroyObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) throws Exception {
    final PooledConnection pc = p.getObject().getPooledConnection();
    pc.removeConnectionEventListener(this);
    pcMap.remove(pc);
    pc.close();
}
Also used : PooledConnection(javax.sql.PooledConnection)

Example 23 with PooledConnection

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

the class KeyedCPDSConnectionFactory method connectionClosed.

// ***********************************************************************
// java.sql.ConnectionEventListener implementation
// ***********************************************************************
/**
     * This will be called if the Connection returned by the getConnection
     * method came from a PooledConnection, and the user calls the close()
     * method of this connection object. What we need to do here is to
     * release this PooledConnection from our pool...
     */
@Override
public void connectionClosed(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    // otherwise return the connection to the pool.
    if (!validatingSet.contains(pc)) {
        final PooledConnectionAndInfo pci = pcMap.get(pc);
        if (pci == null) {
            throw new IllegalStateException(NO_KEY_MESSAGE);
        }
        try {
            _pool.returnObject(pci.getUserPassKey(), pci);
        } catch (final Exception e) {
            System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + "NOT BE RETURNED TO THE POOL");
            pc.removeConnectionEventListener(this);
            try {
                _pool.invalidateObject(pci.getUserPassKey(), pci);
            } catch (final Exception e3) {
                System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
                e3.printStackTrace();
            }
        }
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Example 24 with PooledConnection

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

the class KeyedCPDSConnectionFactory method connectionErrorOccurred.

/**
     * If a fatal error occurs, close the underlying physical connection so as
     * not to be returned in the future
     */
@Override
public void connectionErrorOccurred(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    if (null != event.getSQLException()) {
        System.err.println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" + event.getSQLException() + ")");
    }
    pc.removeConnectionEventListener(this);
    final PooledConnectionAndInfo info = pcMap.get(pc);
    if (info == null) {
        throw new IllegalStateException(NO_KEY_MESSAGE);
    }
    try {
        _pool.invalidateObject(info.getUserPassKey(), info);
    } catch (final Exception e) {
        System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + info);
        e.printStackTrace();
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Example 25 with PooledConnection

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

the class CPDSConnectionFactory method connectionClosed.

// ***********************************************************************
// java.sql.ConnectionEventListener implementation
// ***********************************************************************
/**
     * This will be called if the Connection returned by the getConnection
     * method came from a PooledConnection, and the user calls the close()
     * method of this connection object. What we need to do here is to
     * release this PooledConnection from our pool...
     */
@Override
public void connectionClosed(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    // otherwise return the connection to the pool.
    if (!validatingSet.contains(pc)) {
        final PooledConnectionAndInfo pci = pcMap.get(pc);
        if (pci == null) {
            throw new IllegalStateException(NO_KEY_MESSAGE);
        }
        try {
            _pool.returnObject(pci);
        } catch (final Exception e) {
            System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + "NOT BE RETURNED TO THE POOL");
            pc.removeConnectionEventListener(this);
            try {
                doDestroyObject(pci);
            } catch (final Exception e2) {
                System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
                e2.printStackTrace();
            }
        }
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

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