2

I have tried the solution in WSJDBCConnection does not wrap objects of type Oracle jdbc Connection

but it isn't working so please how do I get the oracle connection out of a tomcat/spring proxy connection pool.

I am using tomcat and a spring datasource and need the oracle connection so I also can pass arrays to my pl/sql

here is my code:

 JdbcTemplate jdbcTemplate = getJdbcTemplate();
 DataSource ds = getDataSource();
 Connection connection = ds.getConnection();
 OracleConnection oracleConnection=connection.unwrap(oracle.jdbc.OracleConnection.class);

My exception is (Thrown on the unwrap):

java.sql.SQLException: Not a wrapper of oracle.jdbc.OracleConnection

When I debug the connection is a:

ProxyConnection[PooledConnection[oracle.jdbc.driver.T4CConnection@4fbe37eb]]

The detail from my context.xml

<ResourceLink name="jdbc/dbname"
                global="jdbc/dbname"
                auth="Container"
                type="javax.sql.DataSource" />  

From my server.xml

<Resource name="jdbc/NPP_npp"
            auth="Container"
            type="javax.sql.DataSource"
            username="XYZ"
            password="NICE_SECURE_PASSWORD"
            driverClassName="oracle.jdbc.driver.OracleDriver"
            url="jdbc:oracle:thin:@//ora-db-dlabnpp.cpp-group.com:1521/devlab_npp.cpp"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
             maxTotal="20" 
              maxIdle="15" 
              minIdle="5"/>

The stacktrace

  org.apache.tomcat.jdbc.pool.ProxyConnection.unwrap(ProxyConnection.java:87)
     org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:119)
     org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
     org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79)
     org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
     org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
     com.sun.proxy.$Proxy148.unwrap(Unknown Source)
     my_project.dao.impl.NppPolicyAcquisitionDAOImpl.createAdditionalDetails(NppPolicyAcquisitionDAOImpl.java:354)
     my_project.dao.impl.NppPolicyAcquisitionDAOImpl.createPolicy(NppPolicyAcquisitionDAOImpl.java:193)
     my_project.service.impl.PolicyAcquisitionServiceImpl.createPolicy(PolicyAcquisitionServiceImpl.java:95)
     my_project.service.rest.PolicyCreationController.createPolicy(PolicyCreationController.java:68)
     my_project.service.rest.PolicyCreationControllerVersionThree.createPolicyVersionThree(PolicyCreationControllerVersionThree.java:95)
     sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     java.lang.reflect.Method.invoke(Method.java:498)
     org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
     org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
     org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
     org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     my_project.filter.CppCertificateIdFilter.doFilter(CppCertificateIdFilter.java:57)
     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     uk.co.cpp.util.spring.web.filter.SpringContextThreadBindingFilter.doFilter(SpringContextThreadBindingFilter.java:60)
     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
     org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
     org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
     org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
     org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
     org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
     org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
     org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
     org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
     org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
     java.lang.Thread.run(Thread.java:748)
7
  • Have you tried the concrete class oracle.jdbc.driver.T4CConnection instead of the interface you are using now? Also can you add the full stack trace so that it is visible where the exception is being thrown from. Finally make sure that your application doesn't contain the oracle drivers, they should be scoped provided if you use maven (as those are provided by tomcat due to the JNDI based datasource), if you include them it will not match the class (class identity is based on classname and class loader!). Commented Oct 1, 2019 at 18:54
  • @M.Deinum, we are using gradle. The exception is on the unwrap line. Commented Oct 1, 2019 at 20:35
  • In your tomcat configuration is there any data sources defined? Commented Oct 2, 2019 at 3:23
  • Please add the full stacktrace, it was requested for a reason. Also as stated, regardless of your build system, the oracle jars shouldn't be part of your application. If you do it will result in classcast exceptions. Commented Oct 2, 2019 at 5:45
  • 1
    You need to have it available for compilation only, not distribute it in your application. If you do that your OracleConnectionin your application won't match the OracleConnection loaded by Tomcat. Both are in different classloaders and while being the same class, they aren't equal (due to different classloaders) and the thus an instanceof check will fail. Leading to these kind of errors. Commented Oct 2, 2019 at 9:46

1 Answer 1

3

You have defined the DataSource with JNDI in Tomcat. Which means Tomcat manages the driver and loads the classes in one of its classloaders. If you also include the Oralce JDBC Driver in your application, the unwrap will fail.

Both OracleConntection classes are in different classloaders and while being the same class, they aren't equal (due to different classloaders) and the thus an instanceof check will fail. Resulting in the error from the unwrap method.

The solution is fairly simply, by excluding the Oracle JDBC driver from your application. You only need it for compilation, at runtime it is provided by the server.

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

1 Comment

I had the same problem with Wildfly. To solve the problem, as described in the response, I changed the Oracle JDBC Driver dependency scope to provided and added the following configuration for the maven-war-plugin (you can also directly add a MANIFEST.MF file in META-INF folder): <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> <manifestEntries> <Dependencies>com.oracle.jdbc</Dependencies> </manifestEntries> </archive>

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.