Getting java.lang.ClassCastException: org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6 cannot be cast to oracle.jdbc.OracleConnection exception while creating temporary CLOB object.

Solution Unverified - Updated -

Environment

  • Oracle JDBC driver

Issue

  • We are getting ClassCastException while creating temporary CLOB object. Following is the code snippet.
 public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException
    {

       CLOB oClob = CLOB.createTemporary(st.getConnection(), Boolean.FALSE,
                CLOB.DURATION_SESSION);

        oClob.open(CLOB.MODE_READWRITE);
        Writer writer = oClob.getCharacterOutputStream();
        try
        {
            writer.write((String) value);
            writer.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        oClob.close();
        st.setClob(index, oClob);
    }
  • We are getting the following exception while executing the above method.
...
...
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168)) Caused by: java.lang.ClassCastException: org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6 cannot be cast to oracle.jdbc.OracleConnection
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at oracle.sql.CLOB.createTemporary(CLOB.java:937)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at oracle.sql.CLOB.createTemporary(CLOB.java:897)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at com.tms.cdqi.data.type.StringClobType.nullSafeSet(StringClobType.java:76)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:158)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2798)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3076)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3515)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:395)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:387)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:303)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:349)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
01:35:19,294 ERROR [stderr] (Thread-59 (HornetQ-client-global-threads-144055168))   at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1159)
...
...

Resolution

  • It seems, this issue is with the connection which is being passed in the following method to create CLOB object.
       CLOB oClob = CLOB.createTemporary(st.getConnection(), Boolean.FALSE, CLOB.DURATION_SESSION);
  • The method CLOB.createTemporary is using java.sql.Connection object as a parameter but it seems, it ONLY works if the Connection is an oracle.jdbc.driver.OracleConnection object. If it's not than ClassCastException would occur.

The reason is, when the code is running in Application Server and trying to getdatabase connectionvia aDataSourceobject, the connection is not a realOracleConnectionobject, but an object that wraps thereal OracleConnectionwith the wrapper class provided by datasource jars. For e.gironjacamar.jdbcadapters` jar.

  • So, to resolve this issue, it is required to extract the original connection from wrapper class as explained in this forum [1] and than verify that both jar should be defined as external module[2] instead defining it inside project lib folder.

Links referred

[1] https://community.oracle.com/message/1087184#1087184
[2] http://stackoverflow.com/questions/10247702/java-lang-classcastexception-org-jboss-jca-adapters-jdbc-jdk6-wrappedconnection

Root Cause

  • The reason is, when the code is running in Application Server and trying to get database connection via a DataSource object, the connection is not a real OracleConnection object, but an object that wraps the real OracleConnection with the wrapper class provided by datasource jars.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments