gpt4 book ai didi

java - 从 datasource.getConnection() 获取 OracleConnection

转载 作者:行者123 更新时间:2023-12-02 09:28:06 27 4
gpt4 key购买 nike

我已经尝试过WSJDBCConnection does not wrap objects of type Oracle jdbc Connection中的解决方案

但它不起作用,所以请问如何从 tomcat/spring 代理连接池中获取 oracle 连接。

我正在使用 tomcat 和 spring 数据源,并且需要 oracle 连接,这样我也可以将数组传递到我的 pl/sql

这是我的代码:

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

我的异常(exception)是(在展开时抛出):

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

当我调试连接时:

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

来 self 的 context.xml 的详细信息

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

来 self 的 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"/>

堆栈跟踪

  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)

最佳答案

您已在 Tomcat 中使用 JNDI 定义了DataSource。这意味着 Tomcat 管理驱动程序并在其类加载器之一中加载类。如果您还在应用程序中包含 Oralce JDBC 驱动程序,unwrap 将失败。

两个 OracleConntection 类位于不同的类加载器中,虽然是同一个类,但它们不相等(由于不同的类加载器),因此 instanceof 检查将失败。导致 unwrap 方法出现错误。

解决方案非常简单,只需从应用程序中排除 Oracle JDBC 驱动程序即可。您仅在编译时需要它,在运行时它由服务器提供。

关于java - 从 datasource.getConnection() 获取 OracleConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58189387/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com