0

I'm facing a weird behavior with tomcat data source and OracleConnection

Case 1:

I have configured datasource in tomcat 8 for oracle12c and placed the ojdbc8.jar in tomcat_home/lib folder. After that I'm executing this below code and it's working

try 
{
    if (connection.isWrapperFor(OracleConnection.class)) 
    {
        oracleConnection = connection.unwrap(OracleConnection.class);  //working fine
    }
} 
catch (SQLException ex) 
{

}

Case 2:

configured datasource again and placed the ojdbc8.jar in tomcat_home/lib folder as well as in WEB-INF/lib of dynamic web project. After that I'm executing same above code and it's not unwrapping the connection and i'm getting null in oracleConnection.

Why is that happening?

Dark Matter
  • 300
  • 2
  • 15
  • 1
    In the failing case, are you saying that isWrapperFor() == true, but the unwrap() returns null? That shouldn't happen, the javadoc for isWrapperFor states: `If this method returns true then calling unwrap with the same argument should succeed.` If you're saying isWrapperFor() == false in the failing case, one possibility is that OracleConnection is being loaded by two different classloaders (one for system, ie tomcat_home/lib and one for app WEB_INF/lib) and the isWrapperFor would see those as two different classes. – F Rowe Dec 05 '19 at 19:45
  • yes, isWrapperFor() is returning false and ```oracleConnection``` is also null. Should I keep ```ojdbc8.jar``` in ```tomcat_home/lib``` or in webapp folder? – Dark Matter Dec 05 '19 at 20:56
  • typically you would keep it in WEB-INF if that is the only app using the driver or you have multiple apps needing different versions, put it in lib if you want to share it with other apps – F Rowe Dec 06 '19 at 00:05
  • Is your ojdbc maven dependency set to provided? If not, you may have two versions of the oracle driver and that is probably the cause of the problem. See: https://stackoverflow.com/questions/42929489/instrumentation-casting-org-apache-tomcat-dbcp-dbcp-poolingdatasourcepoolguard – Kosi Jan 14 '20 at 16:50

1 Answers1

0

Since your tomcat directory already has the dependency, we need to know what your maven (gradle?) dependency looks like. The dependency must be set to scope: provided otherwise you will have duplicate ojdbc drivers.

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc[number_here]</artifactId>
    <version>[version_here]</version>
    <scope>provided</scope>
</dependency>

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

Kosi
  • 263
  • 4
  • 16
  • @FRowe has already explained that, it was the issue of 2 ```ojdbc8.jar``` at both places, in ```tomcat_home/lib``` and in ```webapp``` folder. – Dark Matter Jan 14 '20 at 16:57