Search in sources :

Example 1 with ManagedConnection

use of org.apache.commons.dbcp2.managed.ManagedConnection 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 ManagedConnection

use of org.apache.commons.dbcp2.managed.ManagedConnection in project tomee by apache.

the class DataSourceDefinitionJndiTest method check.

private void check(final DataSource ds, final String name) throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    // the first "cast part" is not important, we just want to check the jdbc url is ok
    assertThat(ds, instanceOf(DbcpManagedDataSource.class));
    final DbcpManagedDataSource dbcp = (DbcpManagedDataSource) ds;
    final Connection connection = dbcp.getConnection();
    assertThat(connection, instanceOf(ManagedConnection.class));
    final ManagedConnection mc = (ManagedConnection) connection;
    final Method getInnermostDelegateInternal = DelegatingConnection.class.getDeclaredMethod("getInnermostDelegateInternal");
    getInnermostDelegateInternal.setAccessible(true);
    final Connection delegate = (Connection) getInnermostDelegateInternal.invoke(mc);
    assertThat(delegate, instanceOf(JDBCConnection.class));
    final Method getURL = JDBCConnection.class.getDeclaredMethod("getURL");
    getURL.setAccessible(true);
    assertEquals("jdbc:hsqldb:mem:" + name, getURL.invoke(delegate));
}
Also used : Connection(java.sql.Connection) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) JDBCConnection(org.hsqldb.jdbc.JDBCConnection) DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) Method(java.lang.reflect.Method) DbcpManagedDataSource(org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource) JDBCConnection(org.hsqldb.jdbc.JDBCConnection)

Example 3 with ManagedConnection

use of org.apache.commons.dbcp2.managed.ManagedConnection in project tomee by apache.

the class DataSourceDefinitionGlobalJPATest method check.

private void check(final DataSource ds, final String name) throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    // the first "cast part" is not important, we just want to check the jdbc url is ok
    assertThat(ds, instanceOf(BasicManagedDataSource.class));
    final BasicManagedDataSource dbcp = (BasicManagedDataSource) ds;
    final Connection connection = dbcp.getConnection();
    assertThat(connection, instanceOf(ManagedConnection.class));
    final ManagedConnection mc = (ManagedConnection) connection;
    final Method getInnermostDelegateInternal = DelegatingConnection.class.getDeclaredMethod("getInnermostDelegateInternal");
    getInnermostDelegateInternal.setAccessible(true);
    final Connection delegate = (Connection) getInnermostDelegateInternal.invoke(mc);
    assertThat(delegate, instanceOf(JDBCConnection.class));
    final Method getURL = JDBCConnection.class.getDeclaredMethod("getURL");
    getURL.setAccessible(true);
    assertEquals("jdbc:hsqldb:mem:" + name, getURL.invoke(delegate));
}
Also used : Connection(java.sql.Connection) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) JDBCConnection(org.hsqldb.jdbc.JDBCConnection) DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) ManagedConnection(org.apache.commons.dbcp2.managed.ManagedConnection) Method(java.lang.reflect.Method) BasicManagedDataSource(org.apache.openejb.resource.jdbc.dbcp.BasicManagedDataSource) JDBCConnection(org.hsqldb.jdbc.JDBCConnection)

Aggregations

ManagedConnection (org.apache.commons.dbcp2.managed.ManagedConnection)3 Method (java.lang.reflect.Method)2 Connection (java.sql.Connection)2 DelegatingConnection (org.apache.commons.dbcp2.DelegatingConnection)2 JDBCConnection (org.hsqldb.jdbc.JDBCConnection)2 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)1 ManagedDataSource (org.apache.commons.dbcp2.managed.ManagedDataSource)1 TransactionRegistry (org.apache.commons.dbcp2.managed.TransactionRegistry)1 BasicManagedDataSource (org.apache.openejb.resource.jdbc.dbcp.BasicManagedDataSource)1 DbcpManagedDataSource (org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource)1