Unwrap to OracleConnection

16,602

I use "oracle.jdbc.pool.OracleConnectionPoolDataSource" as datasouce class in glassfish.

Use the class or find jboss class.

Edit and Try:

public OracleConnection getOracleConnection(Connection connection) throws SQLException {
    OracleConnection oconn = null;
    try {
        if (connection.isWrapperFor(oracle.jdbc.OracleConnection.class)) {
            oconn = (OracleConnection) connection.unwrap(oracle.jdbc.OracleConnection.class)._getPC();
        }
    } catch (SQLException e) {
        throw e;
    }
    return oconn;
}
Share:
16,602
mariu
Author by

mariu

Updated on June 04, 2022

Comments

  • mariu
    mariu over 1 year

    I have this piece of code that used to run properly using JBoss 5.1, Oracle 11, ojdbc6.jar. I was getting the OracleConnection as needed.

    InitialContext ic = new InitialContext();
    DataSource  ds = ( DataSource ) ic.lookup( "java:/" + dataSource );
    Connection con = ds.getConnection();       
    OracleConnection conn = con.unwrap( OracleConnection.class );
    

    Not anymore using JBoss 7, Oracle 11, ojdbc6.jar. It says like this:

    Connection Not a wrapper class for Oracle Connection

    If you have any idea, please help.

  • Viruzzo
    Viruzzo about 12 years
    What exactly doesn't work? I suppose you get a ClassCastException, but where? If it's on the cast to OracleConnection, find out the real class of the object you get with wc.getUnderlyingConnection().
  • Paris
    Paris almost 10 years
    This works indeed, but be aware that you should always close the wrapper Connection and not the unwrapped OracleConnection as this will not release the connection back to the pool. Furthermore, you should not try to close the OracleConnection as this will result in a "connection already closed" exception.