liuming794 发表于 2017-1-23 10:02:03

tomcat数据源Clob出错!

  
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]
查看完整版本: tomcat数据源Clob出错!