Example usage for java.sql Connection isWrapperFor

List of usage examples for java.sql Connection isWrapperFor

Introduction

In this page you can find the example usage for java.sql Connection isWrapperFor.

Prototype

boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException;

Source Link

Document

Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.

Usage

From source file:com.p6spy.engine.common.P6WrapperIsWrapperDelegateTest.java

@Test
public void testCastableFromProxy() throws SQLException {
    Connection con = new TestConnectionImpl();
    Connection proxy = ProxyFactory.createProxy(con, new GenericInvocationHandler<Connection>(con));

    // if the proxy implements the interface then true should be returned.
    assertTrue(proxy.isWrapperFor(Connection.class));
    assertTrue(proxy.isWrapperFor(TestConnection.class));
    assertTrue(proxy.isWrapperFor(Wrapper.class));
}

From source file:com.p6spy.engine.common.P6WrapperIsWrapperDelegateTest.java

@Test
public void testCastableFromUnderlying() throws SQLException {
    Connection con = new TestConnectionImpl();
    Connection proxy = ProxyFactory.createProxy(con, new GenericInvocationHandler<Connection>(con));

    // if the underlying object extends the class (or matches the class) then true should be returned.
    assertTrue(proxy.isWrapperFor(TestConnectionImpl.class));
    assertTrue(proxy.isWrapperFor(AbstractTestConnection.class));
}

From source file:com.p6spy.engine.common.P6WrapperIsWrapperDelegateTest.java

@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);

    Connection proxy = ProxyFactory.createProxy(underlying,
            new GenericInvocationHandler<Connection>(underlying));

    // 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));

}

From source file:com.teradata.benchto.driver.macro.query.QueryMacroExecutionDriver.java

private void runSqlStatements(Connection connection, List<String> sqlStatements) throws SQLException {
    for (String sqlStatement : sqlStatements) {
        sqlStatement = sqlStatement.trim();
        LOGGER.info("Executing macro query: {}", sqlStatement);
        if (sqlStatement.toLowerCase().startsWith(SET_SESSION)
                && connection.isWrapperFor(PrestoConnection.class)) {
            setSessionForPresto(connection, sqlStatement);
        } else {//  w  ww.j  av  a  2 s  .co m
            try (Statement statement = connection.createStatement()) {
                statement.execute(sqlStatement);
            }
        }
    }
}

From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test
public void testWrapper() throws Exception {
    Connection conn = new MyProxy();

    try {/*from   w w w .  j a  v a2s  .  c o m*/
        try {
            conn.isWrapperFor(Connection.class);
        } catch (SQLException e) {

        }
        try {
            conn.unwrap(Connection.class);
        } catch (SQLException e) {

        }
    } finally {
        JdbcUtil.closeQuietly(conn);
    }
}

From source file:net.sf.jasperreports.engine.query.OracleProcedureCallHandler.java

protected boolean isDataDirectDriver() {
    Connection connection;
    try {/*  w w w .  ja  v a  2  s  .c  om*/
        connection = statement.getConnection();
    } catch (SQLException e) {
        log.error("Failure while detecting driver", e);
        return false;
    }

    DatabaseMetaData metaData = null;
    try {
        metaData = connection.getMetaData();
    } catch (SQLException e) {
        log.error("Failure while detecting driver", e);
    }

    String connectionURL = null;
    if (metaData != null) {
        try {
            connectionURL = metaData.getURL();
        } catch (SQLException e) {
            log.error("Failure while detecting driver", e);
        }
    }

    if (connectionURL != null) {
        if (connectionURL.contains(URL_DATADIRECT) || connectionURL.contains(URL_TIBCO)) {
            return true;
        }
        if (connectionURL.contains(URL_ORACLE)) {
            return false;
        }
    }

    if (ORACLE_CONNECTION_CLASS != null) {
        try {
            if (connection.isWrapperFor(ORACLE_CONNECTION_CLASS)) {
                return false;
            }
        } catch (SQLException e) {
            log.error("Failure while detecting driver", e);
        }
    }

    if (metaData != null) {
        try {
            String driverName = metaData.getDriverName();
            if (driverName.equals(DRIVER_NAME_ORACLE)) {
                return false;
            }
            if (driverName.equals(DRIVER_NAME_DATADIRECT)) {
                return true;
            }
        } catch (SQLException e) {
            log.error("Failure while detecting driver", e);
        }
    }

    //fallback to Oracle
    return false;
}

From source file:org.geoserver.data.jdbc.GenericUnWrapperTest.java

@Test
public void testUnwrapConnection() throws SQLException, NoSuchMethodException, SecurityException {
    Connection connection = new TestConnection();
    Connection wrapper = new WrapperConnection(connection);
    assertTrue(wrapper.isWrapperFor(Connection.class));
    Connection unwrap = wrapper.unwrap(Connection.class);

    assertSame(connection, unwrap);//w  w  w .java  2 s.c  o  m

    UnWrapper unwrapper = new GenericUnWrapper();

    assertFalse(unwrapper.canUnwrap(wrapper));
    try {
        assertNull(unwrapper.unwrap(wrapper));
        fail("Cannot unwrap yet");
    } catch (Exception expected) {
    }
    GenericUnWrapper.CONNECTION_METHODS.put(WrapperConnection.class,
            WrapperConnection.class.getMethod("getUnderlyingConnection", null));

    assertTrue(unwrapper.canUnwrap(wrapper));
    assertSame(connection, unwrapper.unwrap(wrapper));
}