1

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

3
  • @mark-rotteveel It's not duplicated, I've read and tried everything, included the question you said it's a duplicated... Commented Mar 21, 2017 at 15:24
  • Have you tried calling getInnermostDelegate? Commented Mar 21, 2017 at 15:29
  • yep, I've tried that as well... Commented Mar 21, 2017 at 15:31

1 Answer 1

6

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);
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Was getting cast exception with PoolingDataSource$PoolGuardConnectionWrapper class. con.unwrap(OracleConnection.class) and set accessToUnderlyingConnectionAllowed="true" under conext.xml fixed the problem.
Had the same issue when connecting to MS SQLServer with com.microsoft.sqlserver.jdbc.SQLServerConnection. Your answer helped a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.