ClassCastException - oracle.jdbc.OraclePreparedStatement

天大地大妈咪最大 提交于 2020-01-02 05:15:10

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/6942763/classcastexception-oracle-jdbc-oraclepreparedstatement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!