Instrumentation: Casting org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper to oracle.jdbc.OracleConnection

Deadly 提交于 2020-01-25 00:22:07

问题


I'm trying to instrument my jdbc connections. I know there are several similar questions about this topic.

I tried everything but couldn't find the propper way to solve my issue so far. Also tried the answers to this question, with no result: Apache Commons DBCP connection object problem, Thread: ClassCastException in org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper

I'm working with Tomcat 7 and Java 7. Here's where I define the oracle connection pool in my context.xml:

<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" />

My instrumentation code:

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

My open connection method:

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

So after calling openConnection and initInstrumentation I'm getting the next exception trying to cast the connection to an oracle connection. Any ideas of how to do this? What am I getting wrong? Thanks in advance.

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection


回答1:


I found my problem. I hope this can help anyone with the same issue.

The thing seems to be related to a conflict with the ojdbc driver libraries. I have one driver in my tomcat, and another one declared in pom.xml via maven.

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

Declaring this driver as provided fixed my problem, and the connection now is been retrieved as described below

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


来源:https://stackoverflow.com/questions/42929489/instrumentation-casting-org-apache-tomcat-dbcp-dbcp-poolingdatasourcepoolguard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!