Search in sources :

Example 1 with PoolableConnection

use of org.apache.commons.dbcp2.PoolableConnection in project tomee by apache.

the class BasicManagedDataSource method createDataSourceInstance.

@Override
protected DataSource createDataSourceInstance() throws SQLException {
    final TransactionRegistry transactionRegistry = getTransactionRegistry();
    if (transactionRegistry == null) {
        throw new IllegalStateException("TransactionRegistry has not been set");
    }
    if (getConnectionPool() == null) {
        throw new IllegalStateException("Pool has not been set");
    }
    final PoolingDataSource<PoolableConnection> pds = new ManagedDataSource<PoolableConnection>(getConnectionPool(), transactionRegistry) {

        @Override
        public Connection getConnection() throws SQLException {
            return new ManagedConnection<PoolableConnection>(getPool(), transactionRegistry, isAccessToUnderlyingConnectionAllowed()) {

                @Override
                public void close() throws SQLException {
                    if (!isClosedInternal()) {
                        try {
                            if (null != getDelegateInternal()) {
                                super.close();
                            }
                        } finally {
                            setClosedInternal(true);
                        }
                    }
                }

                @Override
                public boolean isClosed() throws SQLException {
                    return isClosedInternal() || null != getDelegateInternal() && getDelegateInternal().isClosed();
                }
            };
        }
    };
    pds.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    return pds;
}
Also used : PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) TransactionRegistry(org.apache.commons.dbcp2.managed.TransactionRegistry) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) ManagedDataSource(org.apache.commons.dbcp2.managed.ManagedDataSource)

Example 2 with PoolableConnection

use of org.apache.commons.dbcp2.PoolableConnection in project tomee by apache.

the class DbcpNPEXAConnectionTest method check.

@Test
public void check() throws SQLException {
    final Connection con = ejb.newConn();
    // no NPE
    con.close();
    Assert.assertTrue("Connection was not closed", con.isClosed());
    final GenericObjectPool<PoolableConnection> pool = GenericObjectPool.class.cast(Reflections.get(ds, "connectionPool"));
    assertEquals(0, pool.getNumActive());
}
Also used : PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Connection(java.sql.Connection) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Test(org.junit.Test)

Aggregations

PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)2 Connection (java.sql.Connection)1 ManagedConnection (org.apache.commons.dbcp2.managed.ManagedConnection)1 ManagedDataSource (org.apache.commons.dbcp2.managed.ManagedDataSource)1 TransactionRegistry (org.apache.commons.dbcp2.managed.TransactionRegistry)1 Test (org.junit.Test)1