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?

4
  • 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. Commented Dec 5, 2019 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? Commented Dec 5, 2019 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 Commented Dec 6, 2019 at 0: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: stackoverflow.com/questions/42929489/… Commented Jan 14, 2020 at 16:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@FRowe has already explained that, it was the issue of 2 ojdbc8.jar at both places, in tomcat_home/lib and in webapp folder.

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.