开发者

DBUnit and Oracle JDBC (ClassCastException)

开发者 https://www.devze.com 2023-02-06 08:26 出处:网络
I want to dump my test db into the raw xml file using dbunit and I\'m getting ClassCastException. Below the code开发者_开发问答:

I want to dump my test db into the raw xml file using dbunit and I'm getting ClassCastException. Below the code开发者_开发问答:

new FlatXmlWriter(new FileOutputStream("expected_ds.xml")).
        write(getDbunitConnection().createDataSet(new String[]{"TAB1","TAB2"}));

and as a result :

    java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingResultSet cannot be cast to oracle.jdbc.OracleResultSet

I'm using ojdbc14-10.2.0.3.0.jar, commons-dbcp-1.2.2.jar and dbunit-2.4.7.jar.

Is that a bug in oracle jdbc ? In the ojdbc driver I have found sth like that:

/**
 * 
 * TODO UnitTests are completely missing
 * @author Phil Barr
 * @author Last changed by: $Author: jbhurst $
 * @version $Revision: 1072 $ $Date: 2009-10-12 19:46:45 +0200 (lun, 12 ott 2009) $
 * @since 2.4.0
 */
public class OracleXMLTypeDataType extends BlobDataType
{

    public Object getSqlValue(int column, ResultSet resultSet) throws SQLException,     TypeCastException
    {
        byte[] data = new byte[0];
        OracleResultSet oracleResultSet = (OracleResultSet) resultSet;
        ... some other stuf ...
    }
...
}

It looks like oracle issue and from the javadoc it seems that it was not tested at all. Has anyone had similar problem?


The Oracle JDBC driver is not at fault here.

It looks like DBUnit assumes it can cast the ResultSet to the Oracle-specific type. That's a bad idea in the first place (but can't be avoided in some cases).

Since you're using a connection pool, DbUnit doesn't actually get access to the Oracle-specific object, but to a wrapper provided by the pool instead.

Either stop using a pool for the tests or get the underlying connection from the pooled connection and pass that to DBUnit (this risks that DBUnit closes the physical connection, which is what the pool tries to avoid by only providing the wrapper).


Another option is "Do Not Use Apache DBCP". Instead of Apache use Oracle Datasource

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
        <property name="URL" value="${test.db.url}" />
        <property name="user" value="${test.db.username}"/>
        <property name="password" value="${test.db.password}"/>
        <property name="connectionCachingEnabled" value="true"/>
</bean>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号