设为首页 收藏本站
查看: 666|回复: 0

[经验分享] tomcat数据源Clob出错!

[复制链接]

尚未签到

发表于 2017-1-23 10:02:03 | 显示全部楼层 |阅读模式
  [2009-11-20 14:01:54,093][ERROR][http-8080-Processor23][ErrorHandleFilter.java131]
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
    at oracle.sql.CLOB.createTemporary(CLOB.java:754)
    at oracle.sql.CLOB.createTemporary(CLOB.java:716)
    at com.huawei.wad.ups.common.db.ADMDBSource.executeUpdateWithClobOrBlob(ADMDBSource.java:392)
    at com.huawei.wad.ups.platform.service.datametamanage.LabelRuleService.createBatch(LabelRuleService.java:168)
    at com.huawei.wad.ups.platform.service.datametamanage.LabelRule.createBatch(LabelRule.java:214)
    at com.huawei.wad.ups.platform.servlet.datameta.LabelRuleServlet.createBatch(LabelRuleServlet.java:285)
    at com.huawei.wad.ups.platform.servlet.datameta.LabelRuleServlet.create(LabelRuleServlet.java:161)
    at com.huawei.wad.ups.platform.servlet.BaseServlet.doPost(BaseServlet.java:157)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at com.huawei.wad.ups.platform.servlet.BaseServlet.service(BaseServlet.java:82)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at com.huawei.wad.ups.platform.servlet.filter.AbstractFilter.doFilter(AbstractFilter.java:113)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at com.huawei.wad.ups.platform.servlet.filter.PowerFilter.doFilter(PowerFilter.java:138)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at com.huawei.wad.ups.platform.servlet.filter.ErrorHandleFilter.doFilter(ErrorHandleFilter.java:78)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Thread.java:595)
  网上找:
  ***********************************************************
  异常信息已经很清楚告诉你了

tomcat给你的connection对象包了一层org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper

was给你的connection对象包了一层com.ibm.ws.rsadapter.jdbc.WSJdbcConnection

你的jdbc操作里面肯定使用了一些oracle特定的api,解决方法是不要使用容器提供的连接池,用dbcp获取底层的connection(虽然tomcat用的也是dbcp),那个connection就是你要的,怎么获得自己去dbcp的网站上看吧
  ***********************************************************
  http://forums.oracle.com/forums/thread.jspa?threadID=279238
  I have found the answer myself, and it indeed is what I suspected:

Despite the fact that method CLOB.createTemporary takes a java.sql.Connection object as a parameter, it ONLY works if the Connection is an oracle.jdbc.driver.OracleConnection object. If it's not an oracle.jdbc.driver.OracleConnection, you get a ClassCastException.

That is a BUG in the method CLOB.createTemporary !! (If only an OracleConnection object is allowed, the argument should be of type OracleConnection, not of type java.sql.Connection).

The problem happens because when I run my code in Tomcat and get the database connection via a DataSource object that I lookup via JNDI, the connection is not a real OracleConnection object, but an object that wraps the real OracleConnection.

In more detail:

Tomcat uses the Apache Commons DBCP package (see http://jakarta.apache.org/commons/dbcp/) for database connection pooling. When you lookup a DataSource object using JNDI, like this:

Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/ari");

the object that you get is an instance of class org.apache.commons.dbcp.BasicDataSource. When you call getConnection on this object, you get an instance of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper (which obviously implements java.sql.Connection, and wraps the OracleConnection object).

To be able to call CLOB.createTemporary successfully, we need to find the wrapped OracleConnection object. A number of steps are necessary to reach this:

First of all, by default the PoolingDataSource$PoolGuardConnectionWrapper does not allow access to the wrapped Connection object (don't ask me why, but that's how the Apache developers decided it should be). You must enable access by configuring it in the <Context> of the web application (in Tomcat's server.xml configuration file), for example:

<Context path="/ariweb" docBase="ariweb.war">
<Resource name="jdbc/ari" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/ari">
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<!-- NOTE: This is necessary to enable access to the Oracle connection object -->
<name>accessToUnderlyingConnectionAllowed</name>
<value>true</value>
</parameter>
<!-- Other configuration parameters -->
...
</ResourceParams>
</Context>

The PoolingDataSource$PoolGuardConnectionWrapper extends class org.apache.commons.dbcp.DelegatingConnection, which has a getDelegate method. We can call this method to get the wrapped connection.

Unfortunately, the story is not yet finished. The getDelegate method of the PoolingDataSource$PoolGuardConnectionWrapper does not return the OracleConnection object - it returns an instance of class org.apache.commons.dbcp.PoolableConnection, which is a second wrapper around the OracleConnection object.

The PoolableConnection class also extends DelegatingConnection, so we can call getDelegate again and finally we have the OracleConnection object.

So here is the final code:

// conn is the Connection I got from the DataSource
Connection oracleConnection = conn;

if (conn instanceof org.apache.commons.dbcp.DelegatingConnection) {
// This returns a org.apache.commons.dbcp.PoolableConnection
Connection pc = ((org.apache.commons.dbcp.DelegatingConnection)conn).getDelegate();

// The PoolableConnection is a DelegatingConnection itself - get the delegate (the Oracle connection)
oracleConnection = ((org.apache.commons.dbcp.DelegatingConnection)pc).getDelegate();
}

CLOB clob = CLOB.createTemporary(oracleConnection, true, CLOB.DURATION_SESSION);
clob.open(CLOB.MODE_READWRITE);
...

I hope this helps when you get into the same trouble!

Is there a place on this website where I can file a bug in Oracle's driver?
  ***********************************************************
Re: ClassCastException in method CLOB.createTemporary
Posted: 2004-12-14 上午6:55   http://wv1124.iteye.com/admin/blogs/521180/images/up-10x10.gif in response to: jesper3
 
http://wv1124.iteye.com/admin/blogs/521180/images/warn-16x16.gif  http://wv1124.iteye.com/admin/blogs/521180/images/reply-16x16.gif Reply

I noticed that class org.apache.commons.dbcp.DelegatingConnection has a method called getInnermostDelegate, which returns the innermost wrapped connection, so you could also use the following code:

// conn is the Connection I got from the DataSource
Connection oracleConnection = conn;

if (conn instanceof org.apache.commons.dbcp.DelegatingConnection) {
oracleConnection = ((org.apache.commons.dbcp.DelegatingConnection)conn).getInnermostDelegate();
}

CLOB clob = CLOB.createTemporary(oracleConnection, true, CLOB.DURATION_SESSION);
clob.open(CLOB.MODE_READWRITE);
...

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-332374-1-1.html 上篇帖子: [转]Tomcat调试经验。 下篇帖子: tomcat编码问题根源-2
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表