【问题标题】:Instrumentation: Casting org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper to oracle.jdbc.OracleConnection检测:将 org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper 转换为 oracle.jdbc.OracleConnection
【发布时间】:2017-08-13 06:13:22
【问题描述】:

我正在尝试检测我的 jdbc 连接。我知道关于这个话题有几个类似的问题。

我尝试了所有方法,但到目前为止找不到解决问题的正确方法。 还尝试了这个问题的答案,但没有结果: Apache Commons DBCP connection object problem, Thread: ClassCastException in org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper

我正在使用 Tomcat 7 和 Java 7。这是我在 context.xml 中定义 oracle 连接池的地方:

<Resource name="jdbc/myDS" 
        type="javax.sql.DataSource"
        auth="Container"
        maxActive="350"
        maxIdle="50" 
        minIdle="10" 
        maxWait="10000" 
        username="user_own"
        password="mypassw"
        accessToUnderlyingConnectionAllowed="true"   
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@192.168.110.173:1521/orcl" />

我的检测代码:

private static void initInstrumentation(Connection con, final String usuario, final String modulo, final String accion) throws Exception {

    if (Utils.getParameter("instrumentation.active").equals("1")) {

        try {

            OracleConnection oracleConnection = null;
            //This is where I try to get the oracle connection, but no succeed
            if (con != null) {

                if (con instanceof OracleConnection) {//NEVER COME IN HERE
                    oracleConnection = (OracleConnection) con;
                } else if (con.isWrapperFor(OracleConnection.class)) {//NEVER COME IN HERE
                    oracleConnection = con.unwrap(OracleConnection.class);
                } else{
                    //NO ORACLECONNECTION NO isWrapperFor -> ALWAYS ENDS HERE!!!   
                    //oracleConnection = (OracleConnection)  ((DelegatingConnection) con).getDelegate();
                    oracleConnection = (OracleConnection) new DelegatingConnection(con).getInnermostDelegate();
                }
            }

            if (oracleConnection != null) {
                String[] metrics = new String[OracleConnection.END_TO_END_STATE_INDEX_MAX];
                metrics[OracleConnection.END_TO_END_MODULE_INDEX] = modulo;
                metrics[OracleConnection.END_TO_END_ACTION_INDEX] = "Inicio: " + accion;
                metrics[OracleConnection.END_TO_END_CLIENTID_INDEX] = usuario;

                oracleConnection.setEndToEndMetrics(metrics, (short) 0);
            }

        } catch (Exception e) {
            throw new Exception("Error initInstrumentation " + e);
        }
    }
}

我的开放连接方式:

private static Connection genericOpenConnection() throws Exception {
    Connection con = null;
    try {

        DataSource dataSource = (DataSource) new InitialContext().lookup(Utils.cStrPlx(Utils.getParameter("dataSourceJndiName")));
        con = dataSource.getConnection();
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

    } catch (Exception e) {
        Error.escribeLog("Error : " + e.getMessage());
        throw new SQLException(e.getMessage());
    }
    return con;
}

所以在调用 openConnection 和 initInstrumentation 之后,我遇到了下一个异常,试图将连接强制转换为 oracle 连接。关于如何做到这一点的任何想法?我怎么了? 提前致谢。

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper 无法转换为 oracle.jdbc.OracleConnection

【问题讨论】:

  • @mark-rotteveel 这不是重复的,我已经阅读并尝试了所有内容,包括你说它是重复的问题......
  • 你试过打电话给getInnermostDelegate吗?
  • 是的,我也试过...

标签: oracle jdbc tomcat7 instrumentation ojdbc


【解决方案1】:

我发现了我的问题。我希望这可以帮助任何有同样问题的人。

这件事似乎与 ojdbc 驱动程序库的冲突有关。 我的 tomcat 中有一个驱动程序,另一个通过 maven 在 pom.xml 中声明。

<!-- Driver oracle -->
<dependency>
    <groupId>com.plexus</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0</version>
    <scope>provided</scope>
</dependency>

声明此驱动程序已提供解决了我的问题,现在已按如下所述检索连接

if (con.isWrapperFor(OracleConnection.class)) {
        oracleConnection = con.unwrap(OracleConnection.class);
} 

【讨论】:

  • 正在使用 PoolingDataSource$PoolGuardConnectionWrapper 类获得强制转换异常。 con.unwrap(OracleConnection.class) 并在 conext.xml 下设置 accessToUnderlyingConnectionAllowed="true" 解决了这个问题。
猜你喜欢
  • 2017-07-31
  • 2013-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
  • 2020-10-18
  • 2019-11-18
相关资源
最近更新 更多