[ACCEPTED]-Apache Commons DBCP connection object problem, Thread: ClassCastException in org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper-apache-commons-dbcp

Accepted answer
Score: 25

By default, DBCP does not allow access to 12 the "real" underlying database 11 connection instance, so you cannot get to 10 the Oracle class.

When configuring the pool, you can 9 set

accessToUnderlyingConnectionAllowed = true

and then it works.

Default is false, it 8 is a potential dangerous operation and misbehaving 7 programs can do harmful things. (closing 6 the underlying or continue using it when 5 the guarded connection is already closed) Be 4 careful and only use when you need direct 3 access to driver specific extensions

NOTE: Do 2 not close the underlying connection, only 1 the original one.

Score: 18

If you are using a Java 6 compliant JDBC 3 Connection, you can use code like the following:

OracleConnection oracleConnection = null;
try {
    if (connection.isWrapperFor(OracleConnection.class)) {
        oracleConnection = connection.unwrap(OracleConnection.class);
} catch (SQLException ex) {
    // do something
return oracleConnection;

From 2 this point on, use the oracleConnection instead of the original 1 connection.

See http://docs.oracle.com/javase/6/docs/api/java/sql/Wrapper.html

Score: 6

Seen this post i can get the OracleConnection 2 with this code:

DataSource ds1 = // get the org.apache.commons.dbcp.PoolingDataSource
org.apache.tomcat.dbcp.dbcp.DelegatingConnection del = new org.apache.tomcat.dbcp.dbcp.DelegatingConnection(cds1.getConnection());
OracleConnection con = (OracleConnection) del.getInnermostDelegate();

remember the commons-dbcp-1.4.jar 1 neet to be in the class path

Score: 5

Hmmm,I have meet the same solution like 16 you.I think there a two position need you 15 mention it. 1.Config Connection pool set 14 accessToUnderlyingConnectionAllowed = "true" ; 2.The 13 nightmare concerned to open source project. The 12 terrable conceration. In this case,that is 11


is not equal to


while in default apache 10 common-dbcp.jar,you will never find the 9 follow Class.But just the class is the key. So,we 8 must find the Class in somewhere. I final 7 find the package tomcat-dbcp.jar . You can get it from 6 http://www.docjar.com/ After

import org.apache.tomcat.dbcp.dbcp.DelegatingConnection

,you can force cast you dbConn and 5 get the Underlying Connection

oracle.jdbc.driver.OracleConnection delConn = 



Then we can 4 use delConn to get the ArrayDescriptor Remember 3 one thing,in there,we do not need the

org.apache.commons.dbcp.DelegatingConnection Class

It's 2 a so strange thing,but real work to the 1 case.

Score: 2

I'm positing this here to make sure anyone 21 else looking for advice knows about the 20 ultimate solution to this:

If you're forced 19 to use the non bundled version of the persistence 18 manager (because an old repository still 17 uses that structure which is incompatible 16 with the bundled layout), here what you 15 can do, the solution is quite simple:

Download 14 the sources for Jackrabbit Core (you can 13 get them from the Jackrabbit website) Open 12 the OraclePersistenceManager class and find 11 the following line of code:

Object blob = createTemporary.invoke(null,
                new Object[]{con, Boolean.FALSE, durationSessionConstant});

(Around line 10 377 - can also check the StackTrace for 9 reference)

ConnectionFactory contains a static 8 method that allows to unwrap a connection 7 which is exactly what you need:

Object blob = createTemporary.invoke(null,
                new Object[]{org.apache.jackrabbit.core.util.db.ConnectionFactory
                        .unwrap(con), Boolean.FALSE, durationSessionConstant});

You will 6 need Maven 2+ in order to compile the sources, I 5 did that and had no dependency problems, note 4 that I compiled version 2.2.10 of Jackrabbit.

I 3 also made sure to log a bug against Jackrabbit 2 2.2.11 (current release which still has 1 the issue): https://issues.apache.org/jira/browse/JCR-3262

Hope this helps!

Score: 1

We use arrays in our calls to oracle stored 6 procs and use oracle proprietary api to 5 build arrays. This little check fixed the 4 issue for us when using the functionality 3 from stand alone apps using commons-dbcp.

    if (conn instanceof org.apache.commons.dbcp.DelegatingConnection)
        log.debug("detected apache commons dbcp datasource");
        conn = ((org.apache.commons.dbcp.DelegatingConnection) conn).getInnermostDelegate();

You 2 will need commons-dbcp in the classpath/dependecies 1 though.

Score: 1

I had encountered the same issue. We were 6 using spring and it has a class called NativeJdbcExtractor. It 5 has many implementations and the following 4 one works for TomCat. There are specific 3 implementations for Websphere,Weblogic app 2 servers.

<bean id="jdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"></bean>

In your DAO you can inject the bean 1 and use the following method

protected NativeJdbcExtractor jdbcExtractor;
Connection conn=jdbcExtractor.getNativeConnection(oracleConnection);
Score: 0

in your context definition add below tags 1 to your existing xml definition.



Score: 0

For anyone else looking, getDelegate() and getInnermostDelegate() both return 5 NULL in my code. However, from the debugger 4 I found the OracleConnection as below. We 3 use Spring JdbcTemplate throughout the application, which 2 has the data source injected. We're on spring-jdbc-4.1.5.RELEASE.jar 1 and ojdbc6.jar.

Connection conn = getJdbcTemplate().getDataSource().getConnection();

OracleConnection oracleConnection = ( OracleConnection ) conn.getMetaData().getConnection();
Score: 0

I am working with tomcat 8.5.8 and was facing 6 this issue.
The below solution worked like 5 charm.

The Code:

Delegating Connection delegate_conn = new Delegating Connection(connection)
conn = delegate_conn.getInnermostDelegate();
oracle.sql.ArrayDescriptor desc = oracle.sql.ArrayDescriptor.createDescriptor("TABLE_VIEW", conn);

The Solution:

Adding dependency for tomcat-dbcp 8.5.8 and add the 4 same jar in lib folder of tomcat.
Seems 3 tomcat has different jars for different 2 versions starting from 7.0 (reference: https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-dbcp).

Hope 1 it helps someone.

Score: 0

I'm using java7 & ojdbc7.jar & Tomcat 6 8.

I had the same issue in tomcat while 5 converting ((OracleConnection)connection).createARRAY

After 4 searching for lot of solutions and forums, finally 3 the worked solution for me is,

Connection connection = datasource.getConnection();
CallableStatement cs = connection.prepareCall("{ CALL PKG.PROCEDURE(?)}");

if(cs.getConnection().isWrapperFor(OracleConnection.class)) {
  OracleConnection orConn = cs.getConnection().unwrap(OracleConnection.class);
  orConn.createARRAY ..// is working perfectly.


If you did 2 connection.isWrapperFor(OracleConnection.class) you will get false. you need to use cs.getConnection.

Might 1 help someone.

More Related questions