无法使用 JAVA1.8 和 Tomcat 8.5.28 从 ConnectionWrapper 投射到 oracle.jdbc.OracleConnection [英] Cannot cast from ConnectionWrapper to oracle.jdbc.OracleConnection using JAVA1.8 and Tomcat 8.5.28

查看:38
本文介绍了无法使用 JAVA1.8 和 Tomcat 8.5.28 从 ConnectionWrapper 投射到 oracle.jdbc.OracleConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么几秒钟后连接不起作用?应用程序挂断,未按预期运行,并返回以下错误.

Why connection is not working after few seconds? Application is hanging up and not running as expected and returning the below error.

java.lang.ClassCastException:org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper无法转换为 org.apache.tomcat.dbcp.dbcp2.DelegatingConnection

java.lang.ClassCastException: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to org.apache.tomcat.dbcp.dbcp2.DelegatingConnection

下面是用于获取连接的代码:

Below is the code that is used to get the connection:

OracleConnection oracleConnection = (OracleConnection) 
((DelegatingConnection)connection).getInnermostDelegate();

使用库:commons-pool1.6.jar 用于加密和用于数据库的 tomcat-dbcp.jar.

using libraries: commons-pool1.6.jar for encryption & tomcat-dbcp.jar for database.

在 Tomcat context.xml 中使用加密的用户名和密码.

Using encrypted username and password in Tomcat context.xml.

此外,在 context.xml 文件中使用 accessToUnderlyingConnectionAllowed=true.

Also, using accessToUnderlyingConnectionAllowed=true in context.xml file.

问题在于 JAVA8 和 Tomcat8.能够使用普通凭据正常工作,唯一的问题是加密凭据.

Issue is with JAVA8 and Tomcat8. Able to work properly with plain credentials, the only issue happens with encrypted credentials.

推荐答案

我遇到了同样的问题.经过大量分析后意识到这是类加载问题.通过在 shared.loaded(在 conf/catalina.properties 中)提供 ojdbc jar 修复了该问题

I faced the same issue. After a lot of analysis realized it was classloading issue. Fixed the issue by providing ojdbc jar in shared.loaded (in conf/catalina.properties)

shared.loader="/path/to/ojdbcN_jar/ojdbcN.jar"

这将确保 OracleConnection 类是从 Tomcat 和部署的 web 应用程序中的同一个 jar 加载的.

This will make sure that the OracleConnection classes are loaded from the same jar in Tomcat and in the deployed webapp.

在需要 OracleConnection 的应用程序中,使用以下内容:

And in the application where OracleConnection is needed, use the below:

OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);

注意:在我的应用程序中,我有 ojdbc jar,因此我的应用程序可以正常编译,但是在部署时,使用的 jar 将是共享加载程序中提到的那个.

Note: In my application I have ojdbc jar so that my application compiles fine, but when deployed, the jar used will be the one mentioned in shared loader.

同样不要忘记在创建Tomcat JDBC连接池时开启accessToUnderlyingConnectionAllowed

Also don't forget to enable accessToUnderlyingConnectionAllowed when creating the Tomcat JDBC connection pool

这篇关于无法使用 JAVA1.8 和 Tomcat 8.5.28 从 ConnectionWrapper 投射到 oracle.jdbc.OracleConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆