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.

Community
  • 1
  • 1
user3376818
  • 61
  • 2
  • 8

1 Answers1

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.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thanks Andreas, How to unwrap it. – user3376818 Sep 02 '15 at 23:03
  • Is there any difference in using JDBC Blob over Oracle BLOB – user3376818 Sep 02 '15 at 23:04
  • 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. – Andreas Sep 02 '15 at 23:04
  • JDBC Blob will work with all DBMS's. Don't know if there are any negative sides. – Andreas Sep 02 '15 at 23:05
  • 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) – user3376818 Sep 02 '15 at 23:12
  • Your JDBC driver is too old. Upgrade it. `createBlob` was added in Java 6, so you need a Java 6 compatible version. Better yet, use the latest JDBC driver version that support your Java version. – Andreas Sep 02 '15 at 23:15
  • Thanks,I can't change the version simply, Is there anyway to resolve this issue without changing driver? – user3376818 Sep 02 '15 at 23:20
  • Maybe: Try to cast connection to `org.apache.tomcat.dbcp.dbcp.DelegatingConnection` and call `getDelegate()` or `getInnermostDelegate()`. If they work and return an `oracle.jdbc.OracleConnection`, then you are good to go, but the code will *only* work with Tomcat and may not work on different Tomcat versions. --- It would be *much better* to use a JDBC Driver version that supports the Java version you're running on. – Andreas Sep 02 '15 at 23:29
  • I tried ,but returning null,so Both getDelegate() and getInnermostDelegate both not working – user3376818 Sep 02 '15 at 23:30
  • Then your only option is to upgrade your driver. *Or* stop using `Blob`/`BLOB` and just use a `byte[]`, if not insanely huge. – Andreas Sep 02 '15 at 23:32
  • I am using ojdbc14.jar, Which one is right version to support this functionality – user3376818 Sep 02 '15 at 23:37
  • Download from [here](http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html#). Get either the latest (best option) or the one matching you Database version. The `12.1.0.2` version has a driver for Java 6 (`ojdbc6.jar`) and Java 7 (`ojdbc7.jar`). Use the 7 if you're on Java 8. – Andreas Sep 02 '15 at 23:45
  • By seeing version,I am using latest version.this is very strange issue. – user3376818 Sep 03 '15 at 00:17
  • If you call `conn.getMetaData().getDriverVersion()`, it prints 12.1.0.2 ? --- Anyway, you said you were using `ojdbc14.jar`, which is an old version for Java 1.4! Since you could compile with `createBlob()`, I know you're at least on Java 6. – Andreas Sep 03 '15 at 00:22