cancel
Showing results for 
Search instead for 
Did you mean: 

ClassCastException when casting OracleConnection from data source

Former Member
0 Kudos

Hi

Our application uses a custom data source to manage the connections to the customer's Oracle database.

We need to use proprietary OracleConnection methos, so we cast the generic Connection JDBC to OracleConnection. Example:


@Stateless

public class TestEJB implements TestEJBLocal {

  @Override

    public void testDriver() throws Exception {

  // The data source is obtained from the JNDI

    DataSource ds = (DataSource)new InitialContext().lookup("jdbc/ORACLE_TEST");

  // When casting the Connection a ClassCastException is thrown, and the code fails here

    OracleConnection oc = (OracleConnection) ds.getConnection();

    }

}

At line 12 of the above code, we get the following exception:


Caused by: java.lang.ClassCastException: Cannot cast class com.sap.engine.services.dbpool.cci.ConnectionHandle to interface oracle.jdbc.OracleConnection

We do understand why we are getting the class cast exception (it is explained in this connection management article).

What we don't know is how we can overcome this problem. We need to use an "oracle.jdbc.OracleConnection" object to access proprietary methods (we are dealing with Arrays and other objects).

Can you please suggest options to overcome this? Is some sort of configuration available at the datasource level?

thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Evandro,

we had this problem years ago, so I'm not quite sure about it. But maybe you should just try this.

We added this to application-j2ee-engine.xml:


<reference reference-type="hard" prepend="true">

  <reference-target provider-name="sap.com" target-type="library">Oracle6_11</reference-target>

</reference>

"Oracle6_11" is the resource name of the deployed oracle driver on our server. The "prepend" parameter is not known to the IDE but it is essential.

In Java code, we did this with the connection from the datasource:


final ConnectionHandle connHandle = (ConnectionHandle) connection;

final Connection conn = connHandle.getMetaData().getConnection();

final oracle.jdbc.OracleConnection oracleConn = ((oracle.jdbc.driver.OracleConnection) conn).getPhysicalConnection();

Greetings, Marion

Former Member
0 Kudos

Thank you, that solved our issue! Saved us a lot of trouble!

Took me some time to get back here, but we managed to deploy the application using your suggestion with success one week later.

Answers (1)

Answers (1)

Former Member
0 Kudos

I did mean line 08 of the example code.