0

Trying to create BLOB object using BLOB.createTemporary(connection, false, BLOB.DURATION_SESSION) ,But getting Class Cast Exception

java.lang.ClassCastException: org.apache.commons.dbcp.cpdsadapter.ConnectionImpl cannot be cast to oracle.jdbc.OracleConnection.

I tried following suggestions but still same error .Apache Commons DBCP connection object problem, Thread: ClassCastException in org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper

Some one please suggest me to resolve this issue.

3
  • Please show relevant code, and complete stack trace. Commented Sep 2, 2015 at 22:44
  • BLOB blob = BLOB.createTemporary(delConn, false, BLOB.DURATION_SESSION); Exception :java.lang.ClassCastException: org.apache.commons.dbcp.cpdsadapter.ConnectionImpl cannot be cast to oracle.jdbc.OracleConnection Commented Sep 2, 2015 at 22:55
  • So your program is one line long? What is the type of "delConn"? Commented Sep 2, 2015 at 23:00

1 Answer 1

1

The Oracle BLOB.createTemporary() method expects the Connection parameter to be a oracle.jdbc.OracleConnection object, but connections from Tomcat are managed by DBCP, so the connection is wrapped in a DBCP class (org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper).

You either need to unwrap it to get the real Oracle connection object, or stop using the Oracle BLOB.

Just use the JDBC methods: Blob blob = connection.createBlob()

Update

The JDBC Blob is an interface. There are no implementing classes in the JDK, so you'll always get a DBMS specific implementation. If needed, you can cast to OracleBlob, which is also an interface, that provides additional Oracle-specific methods.


Interesting javadoc for OracleBlob:

Generally any new code should avoid the direct use of the class BLOB. For variable declarations use the interface Blob or this interface as required. Instead of the static methods BLOB.createTemporary(java.sql.Connection, boolean, int) and BLOB.empty_lob() please use Connection.createBlob() and BLOB.getEmptyBLOB() respectively.

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks Andreas, How to unwrap it.
Is there any difference in using JDBC Blob over Oracle BLOB
First option: Don't. You'll be casting to internal Tomcat classes, so your code will only work with Tomcat, and may break on next Tomcat version.
JDBC Blob will work with all DBMS's. Don't know if there are any negative sides.
I tried Blob getting exception : Exception in thread "Thread-4" java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.createBlob()Ljava/sql/Blob; at org.apache.commons.dbcp.DelegatingConnection.createBlob(DelegatingConnection.java:571)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.