java.lang.ClassCastException: com.ibm.ejs.cm.JDBC2PhaseRF
 Technote (FAQ)
 
Problem
Casting an object returned from JNDI™ lookup for a WebSphere® data source to ConnectionPoolDataSource receives "java.lang.ClassCastException: com.ibm.ejs.cm.JDBC2PhaseRF"
 
Solution
Running the following sample code:

   Context ctx = new InitialContext();
ConnectionPoolDataSource cpds = ctx.lookup("jdbc/myDataSource");
receives:
java.lang.ClassCastException: com.ibm.ejs.cm.JDBC2PhaseRF (for 2-Phase Commit data source)
or
java.lang.ClassCastException: com.ibm.ejs.cm.JDBC1PhaseRF (for 1-Phase Commit data source)

                 
Background

The JDBC™ 2.0 API provides a general framework with hooks to support connection pooling instead of specifying a particular connection pooling implementation. This way third party vendors or users have several possibilities for using a particular caching or pooling algorithm to best fit their needs.

The JDBC 2.0 API specifies a class ConnectionEvent and three interfaces ConnectionPoolDataSource, PooledConnection, and ConnectionEventListener, as the hooks for any connection pooling implementations.

Connection pooling should perform completely behind the scenes without affecting application code. The only difference is that the application must use a DataSource object (an object implementing the DataSource interface) instead of the DriverManager class to get a connection. A class implementing the DataSource interface might or might not provide connection pooling. A DataSource object registers with a JNDI naming service. After a DataSource object is registered, the application retrieves it from the JNDI naming service in the standard way.
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/myDataSource");
Explanation & Solution

WebSphere Connection Manager provides connection pooling. It implements the DataSource interface through the JDBC1PhaseRF (for 1-Phase Commit Data Source) and JDBC2PhaseRF (for 2-Phase Commit Data Source) classes. When using WebSphere's DataSource, application code should cast the object returned from JNDI lookup to the DataSource object, not to ConnectionPoolDataSource or to another vendor's specific connection object.

For example:
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/myDataSource");
 
 
 


Document Information


Product categories: Software > Application Servers > Distributed Application & Web Servers > WebSphere Application Server > Java 2 Connectivity (J2C)
Operating system(s): HP-UX
Software version: 4.0
Software edition:
Reference #: 1104478
IBM Group: Software Group
Modified date: Nov 16, 2005