Search code examples
javajdbcapache-commons-dbcp

PoolingDataSource - How to use a driver-specific PreparedStatement implementation


I've implemented the commons dbcp PoolingDataSource and its working perfectly - right up until I ran into an issue of using a specific implementation of the java.sql.PreparedStatement interface.

((OraclePreparedStatement) getStatement()).registerReturnParameter(index, sqlType); 

java.lang.ClassCastException: 
org.apache.commons.dbcp.DelegatingPreparedStatement 
cannot be cast to oracle.jdbc.OraclePreparedStatemen

I understand why this is happening. Is there any way to use a vendor-specific implementation of PreparedStatement but still be able to use the connection pooling that is provided by Commons DBCP? I'd like to use the registerReturnParameter() and getReturnResultSet() which are specific to Oracle's implementation. I know I've violated cardinal rule #1 ...

Also, the PoolingDataSource is wrapping a OracleXADataSource which makes me believe that it's somehow possible to do this...


Solution

  • The name of DelegatingPreparedStatement implies that it simply delegates to the original statement. So you call delecatingPrepartedStatement.getDelegate() which will return the OraclePreparedStatement.

    But really, try not to do this.