0

Background - I was using commons-dbcp 1.2 in my project and I am trying to upgrade to latest commons-dbcp2 2.9 version with ojdbc8 to achieve the resiliency that comes from newer dpcp2 version.

Problem - Post updating to newer dbcp2 I am getting issue while typecasting java.sql.Connection object to dbcp2.PoolableConnection. It is not allowing typecasting throwing below error.

 org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to org.apache.commons.dbcp2.PoolableConnection

Below is my code that I am using.

        CallableStatement stmt;
        Connection connection = stmt.getConnection();

        PoolableConnection poolConn = (PoolableConnection) connection;
        OracleConnection nativeConnection = (OracleConnection) poolConn.getDelegate();

I tried to unwrap the connection like below

PoolableConnection poolConn =(PoolableConnection)connection.unwrap(Connection.class);

But then I am getting error.

oracle.jdbc.driver.T4CConnection cannot be cast to org.apache.commons.dbcp2.PoolableConnection

Can anyone suggest here what are the changes required to pass through this typecasting issue in order to get Poolable Connection from Pool Guard Connection Wrapper. I am new to dbcp2 and need guidance to proceed further.

Thanks in Adavance.

Update- I tried a way to unwrap the PoolGuardWrapper

Connection connection = stmt.getConnection();
            
            Connection unwarppedConnection= connection.unwrap(Connection.class);

            PoolableConnection poolConn = (PoolableConnection)((DelegatingConnection<Connection>) unwarppedConnection).getInnermostDelegate();

OracleConnection nativeConnection = (OracleConnection) poolConn;

But it throwing below error

oracle.jdbc.driver.T4CConnection cannot be cast to org.apache.commons.dbcp2.PoolableConnection
Anshu
  • 69
  • 2
  • 18

1 Answers1

0

I got the solution for this. below are the changes that I made to my code.

CallableStatement stmt;
Connection connection = stmt.getConnection();
Connection updatedConnection =((DelegatingConnection) connection).getInnermostDelegate();

OracleConnection nativeConnection = (OracleConnection) updatedConnection;

I can see the DB transaction now. Let me know If anything I am doing in a wrong way.

Anshu
  • 69
  • 2
  • 18