Search in sources :

Example 16 with PooledConnection

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

the class ConnectionPoolCacheImpl method getNewPoolConnection.

/**
   * Creates a new connection for the pool.
   * 
   * @return the connection from the database as Object.
   * @throws PoolException
   */
@Override
public Object getNewPoolConnection() throws PoolException {
    if (m_cpds != null) {
        PooledConnection poolConn = null;
        try {
            poolConn = m_cpds.getPooledConnection(configProps.getUser(), configProps.getPassword());
        } catch (SQLException sqx) {
            throw new PoolException(LocalizedStrings.ConnectionPoolCacheImpl_CONNECTIONPOOLCACHEIMPLGENEWCONNECTION_EXCEPTION_IN_CREATING_NEW_POOLEDCONNECTION.toLocalizedString(), sqx);
        }
        poolConn.addConnectionEventListener((javax.sql.ConnectionEventListener) connEventListner);
        return poolConn;
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("ConnectionPoolCacheImpl::geNewConnection: ConnectionPoolCache not intialized with ConnectionPoolDatasource");
        }
        throw new PoolException(LocalizedStrings.ConnectionPoolCacheImpl_CONNECTIONPOOLCACHEIMPLGENEWCONNECTION_CONNECTIONPOOLCACHE_NOT_INTIALIZED_WITH_CONNECTIONPOOLDATASOURCE.toLocalizedString());
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Example 17 with PooledConnection

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

the class GemFireConnPooledDataSource method connectionErrorOccurred.

/**
   * Implementation of call back function from ConnectionEventListener interface. This callback will
   * be invoked on connection error event.
   * 
   * @param event
   */
public void connectionErrorOccurred(ConnectionEvent event) {
    if (isActive) {
        try {
            PooledConnection conn = (PooledConnection) event.getSource();
            provider.returnAndExpireConnection(conn);
        } catch (Exception ex) {
            String exception = "GemFireConnPooledDataSource::connectionErrorOccurred:error in returning and expiring connection due to " + ex;
            if (logger.isDebugEnabled()) {
                logger.debug(exception, ex);
            }
        }
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Example 18 with PooledConnection

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

the class GemFireConnPooledDataSource method connectionClosed.

/**
   * Implementation of call back function from ConnectionEventListener interface. This callback will
   * be invoked on connection close event.
   * 
   * @param event
   */
public void connectionClosed(ConnectionEvent event) {
    if (isActive) {
        try {
            PooledConnection conn = (PooledConnection) event.getSource();
            provider.returnConnection(conn);
        } catch (Exception ex) {
            String exception = "GemFireConnPooledDataSource::connectionclosed:Exception =" + ex;
            if (logger.isDebugEnabled()) {
                logger.debug(exception, ex);
            }
        }
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Example 19 with PooledConnection

use of javax.sql.PooledConnection in project spanner-jdbc by olavloite.

the class CloudSpannerPooledConnection method getConnection.

/**
 * Gets a handle for a client to use. This is a wrapper around the physical
 * connection, so the client can call close and it will just return the
 * connection to the pool without really closing the physical connection.
 *
 * <p>
 * According to the JDBC 2.0 Optional Package spec (6.2.3), only one client
 * may have an active handle to the connection at a time, so if there is a
 * previous handle active when this is called, the previous one is forcibly
 * closed and its work rolled back.
 * </p>
 */
@Override
public ICloudSpannerConnection getConnection() throws SQLException {
    if (con == null) {
        // Before throwing the exception, let's notify the registered
        // listeners about the error
        SQLException sqlException = new CloudSpannerSQLException("This PooledConnection has already been closed.", Code.FAILED_PRECONDITION);
        fireConnectionFatalError(sqlException);
        throw sqlException;
    }
    // eliminate bad pooled connections.
    try {
        // Package spec section 6.2.3
        if (last != null) {
            last.close();
            if (!con.getAutoCommit()) {
                rollbackAndIgnoreException();
            }
            con.clearWarnings();
        }
        /*
			 * In XA-mode, autocommit is handled in PGXAConnection, because it
			 * depends on whether an XA-transaction is open or not
			 */
        if (!isXA) {
            con.setAutoCommit(autoCommit);
        }
    } catch (SQLException sqlException) {
        fireConnectionFatalError(sqlException);
        throw (SQLException) sqlException.fillInStackTrace();
    }
    ConnectionHandler handler = new ConnectionHandler(con);
    last = handler;
    ICloudSpannerConnection proxyCon = (ICloudSpannerConnection) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Connection.class, ICloudSpannerConnection.class }, handler);
    last.setProxy(proxyCon);
    return proxyCon;
}
Also used : CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 20 with PooledConnection

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

the class Bug54227 method testPool.

@Test
public void testPool() throws SQLException, InterruptedException {
    PoolProperties poolProperties = new DefaultProperties();
    poolProperties.setMinIdle(0);
    poolProperties.setInitialSize(0);
    poolProperties.setMaxActive(1);
    poolProperties.setMaxWait(5000);
    poolProperties.setMaxAge(100);
    poolProperties.setRemoveAbandoned(false);
    final DataSource ds = new DataSource(poolProperties);
    Connection con;
    Connection actual1;
    Connection actual2;
    con = ds.getConnection();
    actual1 = ((PooledConnection) con).getConnection();
    con.close();
    con = ds.getConnection();
    actual2 = ((PooledConnection) con).getConnection();
    assertSame(actual1, actual2);
    con.close();
    Thread.sleep(150);
    con = ds.getConnection();
    actual2 = ((PooledConnection) con).getConnection();
    assertNotSame(actual1, actual2);
    con.close();
}
Also used : DefaultProperties(org.apache.tomcat.jdbc.test.DefaultProperties) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) Test(org.junit.Test)

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