Unwrap to OracleConnection

2019-08-14 05:07发布

问题:

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.

回答1:

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;
}


回答2:

Try accessing it as a WrappedConnection, like this

Connection con = ds.getConnection();
WrappedConnection wc = (WrappedConnection) con;
OracleConnection conn =  (OracleConnection) wc.getUnderlyingConnection();