oracle 解包到 OracleConnection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8225921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:27:44  来源:igfitidea点击:

Unwrap to OracleConnection

oracleconnectionjboss7.x

提问by mariu

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.

我有这段代码,可以使用 JBoss 5.1、Oracle 11、ojdbc6.jar 正常运行。我正在根据需要获取 OracleConnection。

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:

不再使用 JBoss 7、Oracle 11、ojdbc6.jar。它是这样说的:

Connection Not a wrapper class for Oracle Connection

Connection 不是 Oracle Connection 的包装类

If you have any idea, please help.

如果您有任何想法,请帮忙。

回答by avan

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

我使用“oracle.jdbc.pool.OracleConnectionPoolDataSource”作为 glassfish 中的数据源类。

Use the class or find jboss class.

使用类或查找 jboss 类。

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

回答by Viruzzo

Try accessing it as a WrappedConnection, like this

尝试将其作为 WrappedConnection 访问,如下所示

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