This discussion is archived
8 Replies Latest reply: Nov 10, 2008 10:27 PM by 669738 RSS

Java casting exception when trying to use CLOB.createTemporary()

597647 Newbie
Currently Being Moderated
I'm having an issue attempting to create a temporary CLOB in Java. When I use a connection from apache's connection pool, I get a casting exception from Java (see below)

java.lang.ClassCastException: org.apache.commons.dbcp.PoolableConnection
at oracle.sql.CLOB.createTemporary(CLOB.java:754)
at oracle.sql.CLOB.createTemporary(CLOB.java:715)

When I make my own connection using DriverManager, I do not have any problems passing the connection object to the createTemporary() method of the oracle.sql.CLOB object.

e.g.

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(url, username,password);

if I use this connection, I can create the temporary clob with no problem. I've attempted to use the following bit of code I have found in other examples to pull off any wrapper that apache may be adding to the connection object:

Connection oracleConnection = conn;

if (conn instanceof org.apache.commons.dbcp.DelegatingConnection) {
// This returns a org.apache.commons.dbcp.PoolableConnection
Connection pc = ((org.apache.commons.dbcp.DelegatingConnection)conn).getDelegate();

// The PoolableConnection is a DelegatingConnection itself - get the delegate (the Oracle connection)
oracleConnection = ((org.apache.commons.dbcp.DelegatingConnection)conn).getDelegate();
}

This still doesn't work. When using the above code, I get the same exception albeit in a different form:

java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection

I saw a posting in this forum about this, but none of the information within seems to help/work:

(Re: ClassCastException in method CLOB.createTemporary

If anyone can help shed light on this problem, it would be greatly apprecaited.
  • 1. Re: Java casting exception when trying to use CLOB.createTemporary()
    dsurber Explorer
    Currently Being Moderated
    This is a known problem with the Oracle API. It has been fixed in 11g.

    The problem is that you have to pass a connection to the createTemporary method and that connection must be an instance of an Oracle concrete implementation class. Apache gives you a wrapped connection instead.

    When using the 11g drivers and JDBC 4.0 (JSE 6.0) you can write

    Clob c = conn.createClob();

    This is standard JDBC 4.0 and just works. It is also highly recommended.

    If you are not using 11g, JSE 6.0, JDBC 4.0 then you have to unwrap the connection you get from Apache until you get something that you can cast to oracle.jdbc.OracleConnection. Once you get that object you can pass it to createTemporary and you won't get the ClassCastException.

    This has been a long standing problem in the Oracle JDBC API and I'm glad that it is fixed. In JDBC 4.0/11g there are many factory methods on java.sql.Connection and oracle.jdbc.OracleConnection. Oracle very strongly encourages you to use them instead of calling the constructors. We will desupport the constructors.

    Douglas
  • 2. Re: Java casting exception when trying to use CLOB.createTemporary()
    597647 Newbie
    Currently Being Moderated
    Thank you very much for your response. Unfortunatley, we cannot upgrade to 11g at this time (we just went to 10.2!).

    I have been trying to unwrap the connection and cast it to an oracle connection. Here's the code I've been using. Would you happen to know what I'm doing wrong here? Seems like it should work.

    private CLOB getCLOB(String xmlData, Connection conn) throws SQLException {

    CLOB tempClob = null;
    oracle.jdbc.driver.OracleConnection oconn = null;

    if (conn instanceof org.apache.commons.dbcp.DelegatingConnection) {
    log.info("inside the arena");
    // This returns a org.apache.commons.dbcp.PoolableConnection
    Connection pc = ((org.apache.commons.dbcp.DelegatingConnection)conn).getDelegate();
    // The PoolableConnection is a DelegatingConnection itself - get the delegate (the Oracle connection)
    oconn =(oracle.jdbc.driver.OracleConnection ((org.apache.commons.dbcp.DelegatingConnection)pc).getDelegate();
    }


    The exception is thrown at the last statement when I try to cast to an oracle connection...

    Thanks in advance for any help anyone can lend!
  • 3. Re: Java casting exception when trying to use CLOB.createTemporary()
    dsurber Explorer
    Currently Being Moderated
    You shouldn't be using oracle.jdbc.driver.OracleConnection. Just oracle.jdbc.OracleConnection. Use of the driver package has been deprecated for years and is desupported in 11g.

    Try running

    System.out.println(pc.getClass().getName());

    If that's not an Oracle class, then that's your problem.

    Douglas
  • 4. Re: Java casting exception when trying to use CLOB.createTemporary()
    597647 Newbie
    Currently Being Moderated
    Thanks so much for your help.

    I was finally able to get it working. As it turned out, the version of apache I'm using is still using oracle.jdbc.driver.OracleConnection. When I peeled off the first layer, it went from org.apache.commons.dbcp.PoolableConnection to oracle.jdbc.driver.OracleConnection.

    I was peeling off another layer which is where I found the T4CConnection.

    Peeling off the first layer is all that was necessary. Funny thing tho, I have to use classes12.zip for it to work. If I use ojdbc4.jar, it doesn't work at all. Furthermore, I tried casting both the T4CConnection as well as the oracle.jdbc.driver.OracleConnection to the newer oracle.jdbc.OracleConnection and it wouldn't work either.

    Nonetheless, thanks again for all your help!
  • 5. Re: Java casting exception when trying to use CLOB.createTemporary()
    dsurber Explorer
    Currently Being Moderated
    I think you may have multiple versions of Oracle JDBC in your classpath. If you have just one ojdbc14.jar in your classpath and no other Oracle JDBC jar files, including classes12.jar, casting a T4CConnection to oracle.jdbc.OracleConnection will work. The only way I can imagine that it wouldn't work is if you were loading T4CConnection from one jar and OracleConnection from another. That will cause all kinds of problems.

    Douglas
  • 6. Re: Java casting exception when trying to use CLOB.createTemporary()
    dsurber Explorer
    Currently Being Moderated
    I was finally able to get it working. As it turned
    out, the version of apache I'm using is still using
    oracle.jdbc.driver.OracleConnection. When I peeled
    off the first layer, it went from
    org.apache.commons.dbcp.PoolableConnection to
    oracle.jdbc.driver.OracleConnection.
    The driver still uses oracle.jdbc.driver.OracleConnection internally, but customer code cannot and should not use that class name. We deprecated customer use of the oracle.jdbc.driver package. We did not remove the package. When you get a stack trace you'll see lots of driver.* class names, but most of those classes are now package visible which means your code will not compile or run if you use them since they aren't visible to customer code. This in in 11g.

    If the version of Apache you are using is returning instances of oracle.jdbc.driver.OracleConnection, then it is using a very old version of Oracle JDBC because it has been an abstract class for several releases, I think since 10.1. To be clear, I'm distinguishing between seeing oracle.jdbc.driver.OracleConnection in a stack trace, which will happen even in 11g and getting an instance of oracle.jdbc.driver.OracleConnection which can only happen in 9i and older drivers.

    Douglas
  • 7. Re: Java casting exception when trying to use CLOB.createTemporary()
    626811 Newbie
    Currently Being Moderated
    Take a look at this blog. You will see a step by step example.

    http://ravivedala.blogspot.com/2008/03/oracle-10g-passing-arrays-to-database.html

    The nightmare exception for Java/Oracle developers :-)

    java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection cannot be cast to oracle.jdbc.OracleConnection
    at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:149)
    at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:115)
    ...........

    Solution :
    a)You will see the above exception, if you have ojdbc14.jar in your war file. You would be having ojdbc14.jar on your classpath for compiling your java classes. Use it only for compilation. Don't include it in the build to Tomcat. ie., the war file of your web app should NOT have ojdbc14.jar in it.

    b) Make sure that the Oracle thin driver (eg : ojdbc14.jar) is in tomcat's common\lib.

    Good Luck !!
    r-a-v-i
  • 8. Re: Java casting exception when trying to use CLOB.createTemporary()
    669738 Newbie
    Currently Being Moderated
    use the method getInnermostDelegate() instead of getDelegate() which will provide you oracle.jdbc.OracleConnection object.

    This will work :)