Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException - oracle.jdbc.OraclePreparedStatement

I'm encountering an aggravating issue, here's the facts -

I'm registering a return parameter specific and to do so I'm casting a java.sql.PreparedStatement to oracle.jdbc.OraclePreparedStatement.

((OraclePreparedStatement) getStatement())
    .registerReturnParameter(index, sqlType);   

This works great when I run this from Eclipse and it even runs as expected on our development server. However, it's when I move it to our testing server where I hit an unexpected error...

oracle.jdbc.driver.OraclePreparedStatementWrapper cannot be cast
 to oracle.jdbc.OraclePreparedStatement

It's incredibly strange error to me because I'm sure that OraclePreparedStatement is assignable from getStatement(). I've debugged and found that this is TRUE for all environments:

//class oracle.jdbc.driver.OraclePreparedStatementWrapper
getStatement().getClass();

LOCAL and DEV environments both use a DataSource I've set up in META-INF/context.xml:

<Resource name="dataSource/dbsubm" auth="Container"
    type="oracle.jdbc.xa.client.OracleXADataSource"
    factory="org.apache.naming.factory.BeanFactory"
    user="*****" password="******"
    URL="jdbc:oracle:thin:@host:port:db" />

TEST environment differs because it has a DataSource coming from server.xml even though the configuration is exactly the same. This to me is the only difference between these environments.

What could be the issue? Why do I get a ClassCastException using the same code but different environments? Using getClass() I can tell that they are all the same type... please help!

like image 971
John Strickler Avatar asked Aug 04 '11 13:08

John Strickler


1 Answers

A ClassCastException can occur if the cast crosses classloader boundaries. For example, if the returned statement object's class was loaded by a classloader different from the one that loaded OraclePreparedStatemen in your code. This can be caused by having two separate copies of the JDBC jar in two places, one of which is being used by your Java EE container (Tomcat? WAS?) and the other by your code.

like image 198
Jim Garrison Avatar answered Nov 13 '22 05:11

Jim Garrison