public Connection getConnection() throws SQLException { return new org.apache.commons.dbcp.DelegatingConnection( java.sql.DriverManager.getConnection( jdbcConnectString, jdbcProperties)); }
@Test public void testProxyOfWrappedConnection() throws SQLException { // this will be the actual connection Connection con = new TestConnectionImpl(); // use a wrapper from DBCP to create a proxy of a proxy // Note: DBCP implements with JDBC 4.0 API so the Wrapper interface // is implemented here. DelegatingConnection underlying = new DelegatingConnection(con); try (Connection proxy = ConnectionWrapper.wrap(con, noOpEventListener, ConnectionInformation.fromTestConnection(con))) { // TestConnection is an interface of the wrapped underlying object. assertTrue(proxy.isWrapperFor(TestConnection.class)); // ResultSet is not implemented at all - false should be returned assertFalse(proxy.isWrapperFor(ResultSet.class)); } }
@Test public void testProxyOfWrappedConnection() throws SQLException { // this will be the actual connection Connection con = new TestConnectionImpl(); // use a wrapper from DBCP to create a proxy of a proxy // Note: DBCP implements with JDBC 4.0 API so the Wrapper interface // is implemented here. DelegatingConnection underlying = new DelegatingConnection(con); try (Connection proxy = ConnectionWrapper.wrap(con, noOpEventListener, ConnectionInformation.fromTestConnection(underlying))) { // TestConnection is an interface of the actual connection but not of the proxy. Unwrapping works // but a proxy is not returned { TestConnection unwrapped = proxy.unwrap(TestConnection.class); assertFalse(AbstractWrapper.isProxy(unwrapped)); } // ResultSet is not implemented at all - an exception will be thrown try { proxy.unwrap(ResultSet.class); fail("Expected exception not thrown"); } catch (SQLException e) { } } }
/** * Wrap the real connection. */ PooledConnectionImpl(Connection connection, KeyedObjectPool pool) { this.connection = connection; if (connection instanceof DelegatingConnection) { this.delegatingConnection = (DelegatingConnection) connection; } else { this.delegatingConnection = new DelegatingConnection(connection); } eventListeners = new Vector(); isClosed = false; if (pool != null) { pstmtPool = pool; pstmtPool.setFactory(this); } }
/** * Wrap the real connection. * @param connection the connection to be wrapped * @param pool the pool to use */ PooledConnectionImpl(Connection connection, KeyedObjectPool pool) { this.connection = connection; if (connection instanceof DelegatingConnection) { this.delegatingConnection = (DelegatingConnection) connection; } else { this.delegatingConnection = new DelegatingConnection(connection); } eventListeners = new Vector(); isClosed = false; if (pool != null) { pstmtPool = pool; pstmtPool.setFactory(this); } }
/** * Wrap the real connection. * @param connection the connection to be wrapped * @param pool the pool to use */ PooledConnectionImpl(Connection connection, KeyedObjectPool pool) { this.connection = connection; if (connection instanceof DelegatingConnection) { this.delegatingConnection = (DelegatingConnection) connection; } else { this.delegatingConnection = new DelegatingConnection(connection); } eventListeners = new Vector(); isClosed = false; if (pool != null) { pstmtPool = pool; pstmtPool.setFactory(this); } }