Search in sources :

Example 11 with PooledConnection

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

the class AbstractPoolCacheJUnitTest method testValidateConnection.

/**
   * Test of validateConnection method, of class
   * org.apache.geode.internal.datasource.AbstractPoolCache.
   */
@Test
public void testValidateConnection() throws Exception {
    Context ctx = cache.getJNDIContext();
    GemFireConnPooledDataSource ds = (GemFireConnPooledDataSource) ctx.lookup("java:/PooledDataSource");
    GemFireConnectionPoolManager provider = (GemFireConnectionPoolManager) ds.getConnectionProvider();
    ConnectionPoolCacheImpl poolCache = (ConnectionPoolCacheImpl) provider.getConnectionPoolCache();
    PooledConnection poolConn = (PooledConnection) poolCache.getPooledConnectionFromPool();
    Connection conn = poolConn.getConnection();
    if (!ds.validateConnection(conn))
        fail("validate connection failed");
    conn.close();
    if (ds.validateConnection(conn))
        fail("validate connection failed");
}
Also used : Context(javax.naming.Context) PooledConnection(javax.sql.PooledConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with PooledConnection

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

the class AbstractPoolCacheJUnitTest method testReturnPooledConnectionToPool.

/**
   * Test of returnPooledConnectionToPool method, of class
   * org.apache.geode.internal.datasource.AbstractPoolCache.
   */
@Test
public void testReturnPooledConnectionToPool() throws Exception {
    Context ctx = cache.getJNDIContext();
    GemFireConnPooledDataSource ds = (GemFireConnPooledDataSource) ctx.lookup("java:/PooledDataSource");
    GemFireConnectionPoolManager provider = (GemFireConnectionPoolManager) ds.getConnectionProvider();
    ConnectionPoolCacheImpl poolCache = (ConnectionPoolCacheImpl) provider.getConnectionPoolCache();
    PooledConnection conn = (PooledConnection) poolCache.getPooledConnectionFromPool();
    if (poolCache.availableCache.containsKey(conn))
        fail("connection not removed from available cache list");
    if (!poolCache.activeCache.containsKey(conn))
        fail("connection not put in active connection list");
    provider.returnConnection(conn);
    if (!poolCache.availableCache.containsKey(conn))
        fail("connection not returned to pool");
    if (poolCache.activeCache.containsKey(conn))
        fail("connection not returned to active list");
}
Also used : Context(javax.naming.Context) PooledConnection(javax.sql.PooledConnection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with PooledConnection

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

the class AbstractPoolCacheJUnitTest method testGetPooledConnectionFromPool.

/**
   * Test of getPooledConnectionFromPool method, of class
   * org.apache.geode.internal.datasource.AbstractPoolCache.
   */
@Test
public void testGetPooledConnectionFromPool() throws Exception {
    Context ctx = cache.getJNDIContext();
    GemFireConnPooledDataSource ds = (GemFireConnPooledDataSource) ctx.lookup("java:/PooledDataSource");
    GemFireConnectionPoolManager provider = (GemFireConnectionPoolManager) ds.getConnectionProvider();
    ConnectionPoolCacheImpl poolCache = (ConnectionPoolCacheImpl) provider.getConnectionPoolCache();
    PooledConnection poolConn = (PooledConnection) poolCache.getPooledConnectionFromPool();
    if (poolConn == null)
        fail("getPooledConnectionFromPool failed to get a connection from pool");
}
Also used : Context(javax.naming.Context) PooledConnection(javax.sql.PooledConnection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 14 with PooledConnection

use of javax.sql.PooledConnection in project cdap by caskdata.

the class DBConnectionPoolManager method getValidConnection2.

// CHECKSTYLE ON
private Connection getValidConnection2(long time, long timeoutTime) {
    long rtime = Math.max(1, timeoutTime - time);
    Connection conn;
    try {
        conn = getConnection2(rtime);
    } catch (SQLException e) {
        return null;
    }
    rtime = timeoutTime - System.currentTimeMillis();
    int rtimeSecs = Math.max(1, (int) ((rtime + 999) / 1000));
    try {
        if (conn.isValid(rtimeSecs)) {
            return conn;
        }
    } catch (SQLException e) {
    }
    // This Exception should never occur. If it nevertheless occurs,
    // it's because of an error in the JDBC driver which we ignore and assume
    // that the connection is not valid. When isValid() returns false, the JDBC
    // driver should have already called  connectionErrorOccurred()
    // and the PooledConnection has been removed from the pool, i.e. the
    // PooledConnection will not be added to recycledConnections when
    // Connection.close() is called. But to be sure that this works even with
    // a faulty JDBC driver, we call purgeConnection().
    purgeConnection(conn);
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection)

Example 15 with PooledConnection

use of javax.sql.PooledConnection in project cdap by caskdata.

the class DBConnectionPoolManager method getConnection3.

private synchronized Connection getConnection3() throws SQLException {
    // test again within synchronized lock
    if (isDisposed) {
        throw new IllegalStateException("Connection pool has been disposed.");
    }
    PooledConnection pconn;
    if (!recycledConnections.isEmpty()) {
        pconn = recycledConnections.remove();
    } else {
        pconn = dataSource.getPooledConnection();
        pconn.addConnectionEventListener(poolConnectionEventListener);
    }
    Connection conn;
    try {
        // The JDBC driver may call ConnectionEventListener.connectionErrorOccurred()
        // from within PooledConnection.getConnection(). To detect this within
        // disposeConnection(), we temporarily set connectionInTransition.
        connectionInTransition = pconn;
        conn = pconn.getConnection();
    } finally {
        connectionInTransition = null;
    }
    activeConnections++;
    assertInnerState();
    return conn;
}
Also used : PooledConnection(javax.sql.PooledConnection) 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