Wednesday, 27 February 2008

ClassCastException casting to OracleConnection from a Data Source Connection in OAS 10.1.3.x

I have previously blogged how to access a data source remotely using OAS 10.1.3.x as shown below.

http://theblasfrompas.blogspot.com/2007/07/access-data-source-from-oc4j-remotely.html


Normally I cast back to a java.sql.Connection but you can also cast to an OracleConnection, as shown below.

OracleConnection oraConnection = null;
InitialContext ic = new InitialContext(env);
DataSource ds = (DataSource) ic.lookup("jdbc/scottDS");
oraConnection = (OracleConnection) ds.getConnection();

When you do this you may get a runtime exception as follows:

Exception in thread "main" java.lang.ClassCastException: oracle_jdbc_driver_LogicalConnection_Proxy

The problem here is you must import the correct OracleConnection. The one you want to import is "oracle.jdbc.OracleConnection". You end up with the error above if you use "oracle.jdbc.driver.OracleConnection".

6 comments:

Neil said...

Thanks, I found that useful. Helped me resolve an error.

Alan said...

Thanks for the tip. Oracle loves to move away from the standard.

Pas Apicella said...

It is for a good reason here:

OracleConnection extends standard JDBC connection functionality to create and return Oracle statement objects, set flags and options for Oracle performance extensions, and support type maps for Oracle objects.

Alejandro said...

My name is Alex Meza from Mexico and I’m an Oracle Implementer for a client that is @On Demand. I had a call with you about two year ago about another issue, so it’s good to know from you again.

Right now we’re trying to upgrade our jdbc driver and getting this class cast exception error. We are using j2ee applications developed on ADF Faces BC4J Framework and we recently update jdbc driver as per metalink note: 420303.1, but after upgrading driver all applications stop working and in the log files we’re getting this errors.

Our applications connect to database using BC4J Modules configured to use datasources. It seems that somewhere in the server side we need to change class oracle.jdbc.driver.OracleConnection for oracle.jdbc.OracleConnection. On OAS 10.1.2 you can do it on the datasource configuration but it don’t see it on OAS 10.1.3.

Where is this class located on datasources configuration using OAS 10.1.3?


Best Regards
Alex Meza

Pas Apicella said...

You more then likely are using a managed data source which itself refers to a connection pool which is what we recommend in OAS 10.1.3.x, it's the connection pool you need to alter , most will use one of the following.

A) factory-class="oracle.jdbc.OracleDriver"

OR

B) factory-class="oracle.jdbc.pool.OracleDataSource"

Note: The managed data source will reference the connection pool it's using so you know which connection pool to edit as shown below.

More information can be found here.

http://download-uk.oracle.com/docs/cd/B31017_01/web.1013/b28958/datasrc.htm#CHDJIHBH

mahendra said...

Thanks, thats really helpful.