InitialContext ctx = new InitialContext(parms);
DataSource ds = (DataSource)ctx.lookup(jndi);
Connection conn = ds.getConnection();
if (conn.isWrapperFor(oracle.jdbc.OracleConnection.class)) {
OracleConnection oraCon=conn.unwrap(oracle.jdbc.OracleConnection.class);
// PERFORM ORACLE DRIVER SPECIFIC CALLS USING oraCon .....
}
conn.close();
Now the above code snippet will work like a charm in most environments. But at times there is a chance to hit the following exception if you unwrap without doing a isWrapperFor() check.
java.sql.SQLException: DSRA9122E: com.ibm.ws.rsadapter.jdbc.WSJdbcConnection@d3t7e556 does not wrap any objects of type oracle.jdbc.OracleConnection.
The most possible cause of this problem is due to classloader mismatch and for the unwrap to work the specified interface class (ie oracle.jdbc.OracleConnection) must be loaded with the same classloader that is used by the dataSource/jdbcDriver. And how this mismatch can happen ?
.When you have applications packaging the JDBC driver jars along with them in addition to the the JDBC driver classpath provided in the datasource JDBC provider configuration, then a classloader mismatch is very much possible. Always avoid packing jdbc driver jars within the application.
Note: These are Nihilson's personal views and does not reflect that of his employer in any way!!