0
votes

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.

1
Please show relevant code, and complete stack trace.OldProgrammer
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.OracleConnectionuser3376818
So your program is one line long? What is the type of "delConn"?OldProgrammer

1 Answers

1
votes

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.