Java Code Examples for java.sql.Connection.unwrap()

The following are Jave code examples for showing how to use unwrap() of the java.sql.Connection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: spanner-jdbc   File: CloudSpannerDataSourceTest.java   View Source Code Vote up 6 votes
@Test
public void createDataSourceTest() throws SQLException
{
	CloudSpannerDataSource subject = new CloudSpannerDataSource();
	subject.setProjectId("helpful-adroit-123456");
	subject.setInstanceId("test-instance");
	subject.setDatabase("test");
	subject.setPvtKeyPath("C:\\Users\\MyUserName\\Documents\\CloudSpannerKeys\\cloudspanner-key.json");
	subject.setPvtKeyPath(null);
	subject.setOauthAccessToken("TEST");
	subject.setSimulateProductName("PostgreSQL");
	subject.setAllowExtendedMode(true);
	subject.setLoginTimeout(10);
	subject.setLogWriter(new PrintWriter(System.out));
	Connection con = subject.getConnection();
	Assert.assertNotNull(con);
	Assert.assertTrue(con.isWrapperFor(CloudSpannerConnection.class));
	CloudSpannerConnection connection = con.unwrap(CloudSpannerConnection.class);
	Assert.assertEquals("jdbc:cloudspanner://localhost", connection.getUrl());
	Assert.assertEquals("PostgreSQL", connection.getProductName());
	Assert.assertEquals("helpful-adroit-123456", connection.getSuppliedProperties().getProperty("Project"));
	Assert.assertEquals("test-instance", connection.getSuppliedProperties().getProperty("Instance"));
	Assert.assertEquals("test", connection.getSuppliedProperties().getProperty("Database"));
	Assert.assertEquals("TEST", connection.getSuppliedProperties().getProperty("OAuthAccessToken"));
	Assert.assertTrue(connection.isAllowExtendedMode());
}
 
Example 2
Project: tomcat7   File: TestSuspectTimeout.java   View Source Code Vote up 6 votes
@Test
public void testSuspect() throws Exception {
    this.datasource.setMaxActive(100);
    this.datasource.setMaxIdle(100);
    this.datasource.setInitialSize(0);
    this.datasource.getPoolProperties().setAbandonWhenPercentageFull(0);
    this.datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(100);
    this.datasource.getPoolProperties().setRemoveAbandoned(true);
    this.datasource.getPoolProperties().setRemoveAbandonedTimeout(100);
    this.datasource.getPoolProperties().setSuspectTimeout(1);
    this.datasource.getPoolProperties().setLogAbandoned(true);
    Connection con = datasource.getConnection();
    Assert.assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
    Thread.sleep(3000);
    PooledConnection pcon = con.unwrap(PooledConnection.class);
    Assert.assertTrue("Connection should be marked suspect",pcon.isSuspect());
    con.close();
}
 
Example 3
Project: apache-tomcat-7.0.73-with-comment   File: TestSuspectTimeout.java   View Source Code Vote up 6 votes
@Test
public void testSuspect() throws Exception {
    this.datasource.setMaxActive(100);
    this.datasource.setMaxIdle(100);
    this.datasource.setInitialSize(0);
    this.datasource.getPoolProperties().setAbandonWhenPercentageFull(0);
    this.datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(100);
    this.datasource.getPoolProperties().setRemoveAbandoned(true);
    this.datasource.getPoolProperties().setRemoveAbandonedTimeout(100);
    this.datasource.getPoolProperties().setSuspectTimeout(1);
    this.datasource.getPoolProperties().setLogAbandoned(true);
    Connection con = datasource.getConnection();
    Assert.assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
    Thread.sleep(3000);
    PooledConnection pcon = con.unwrap(PooledConnection.class);
    Assert.assertTrue("Connection should be marked suspect",pcon.isSuspect());
    con.close();
}
 
Example 4
Project: jaffa-framework   File: JDBCSecurityPlugin.java   View Source Code Vote up 5 votes
protected void executeStoredProcedure(final Connection pooledConnection, final String userid) throws java.sql.SQLException {
    CallableStatement cs = null;

    final String localId = (String) ContextManagerFactory.instance().getProperty(LOCAL_ID);
    Object[] roles = getUserRoles(userid).toArray();
    ARRAY newArray = null;

    Connection connection = null;
    try {
        // Extract the actual oracle connection
        connection = pooledConnection.unwrap(oracle.jdbc.OracleConnection.class);
        if (!(connection instanceof oracle.jdbc.OracleConnection)) {
        	throw new Exception(String.format("Not an Oracle Connection : %s",connection.getClass().getName()));
        }

        cs = connection.prepareCall(CALL_JAFFA_SEC_SET_USERID);
        cs.setString(1,userid);

        newArray = ((oracle.jdbc.OracleConnection)connection).createARRAY(ROLE,roles);
        cs.setArray(2,newArray);
        cs.setString(3,localId);

        if (log.isDebugEnabled()) {
            // Log the call to the stored procedure
            debugStatement(userid, localId, roles, newArray);
        }

        if(log.isDebugEnabled()) log.debug("Calling the Stored Procedure to Set the Context");
        cs.execute();
        connection.commit();
    }
    catch (Exception exception) {
    	final String message = String.format(String.format(SET_THE_CONTEXT_ERROR_MESSAGE,exception.getMessage()));
        log.error(message, exception);
        if (connection != null)
        	connection.rollback();
        throw new UOWSecurityException(message, exception);
    }
    finally {
        roles = null;
        if (newArray != null)
            newArray.free();
        if (cs != null) {
            cs.clearParameters();
            cs.close();
            cs = null;
        }
    }
}
 
Example 5
Project: jaffa-framework   File: JDBCSecurityPlugin.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 */
@Override
public boolean isSecurableConnection(Connection connection) {
    try {
        return connection != null && connection.unwrap(oracle.jdbc.OracleConnection.class) instanceof oracle.jdbc.OracleConnection;
    }catch (SQLException e){
        return false;
    }
}
 
Example 6
Project: syndesis   File: SqlIconFileStore.java   View Source Code Vote up 4 votes
private PGConnection getPostgresConnection(Connection conn) throws SQLException {
    if (conn instanceof PGConnection) {
        return PGConnection.class.cast(conn);
    }
    return conn.unwrap(PGConnection.class);
}
 
Example 7
Project: syndesis   File: SqlExtensionFileStore.java   View Source Code Vote up 4 votes
private PGConnection getPostgresConnection(Connection conn) throws SQLException {
    if (conn instanceof PGConnection) {
        return PGConnection.class.cast(conn);
    }
    return conn.unwrap(PGConnection.class);
}
 
Example 8
Project: lams   File: Jdbc4NativeJdbcExtractor.java   View Source Code Vote up 4 votes
@Override
protected Connection doGetNativeConnection(Connection con) throws SQLException {
	return con.unwrap(this.connectionType);
}