JDBC driver cannot be cast to javax.sql.DataSource or javax.sql.XADataSource in JBoss EAP
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 6
- 7
Issue
-
Encountering a cast error using
OracleDriver... IJ000604: Throwable while attempting to get a new connection: null: javax.resource.ResourceException: IJ031084: Unable to create connection ... Caused by: javax.resource.ResourceException: IJ031089: Failed to load datasource: oracle.jdbc.driver.OracleDriver ... Caused by: java.lang.ClassCastException: oracle.jdbc.driver.OracleDriver cannot be cast to javax.sql.DataSource ... -
Encountering a cast error using
DB2Driver... IJ000453: Unable to get managed connection for java:java/myds ... Caused by: javax.resource.ResourceException: IJ000658: Unexpected throwable while trying to create a connection: null ... Caused by: javax.resource.ResourceException: Could not create connection ... Caused by: java.lang.ClassCastException: com.ibm.db2.jcc.DB2Driver cannot be cast to javax.sql.XADataSource ...
Resolution
It may be helpful to review the use of DataSource, XADataSource and Driver implementations for JDBC access in JBoss EAP for context on the discussion below.
-
Avoid the use of the
<datasource-class>property where possible as this is usually unnecessary. -
If using only
xa-datasourceuse only thexa-datasource-classproperty.- Note that the class specified using the
<xa-datasource-class>property must implement the Content from docs.oracle.com is not included.javax.sql.XADataSource interface.
- Note that the class specified using the
-
If using (non-XA)
datasourcepools use one of the following approaches:3.1. If the JDBC driver JAR does not declare a driver class in
META-INF/services/java.sql.Driver, specify an appropriate vendor specific class explicitly using the<driver-class>property if the 3rd party jar includes aDriverimplementation.- Note that the class specified using the
<driver-class>property must implement the Content from docs.oracle.com is not included.java.sql.Driver interface.
3.2. If the JDBC driver JAR includes no driver class, specify an appropriate vendor specific datasource class using the
<datasource-class>property.- Note that the class specified using the
<datasource-class>property must implement the Content from docs.oracle.com is not included.javax.sql.DataSource interface.
- Note that the class specified using the
-
It is recommended that the
<driver-class>,<datasource-class>, and/or<xa-datasource-class>properties be specified (once) within a<driver>entry in the<drivers>configuration rather than replicating the properties in each<datasource>...</datasource>or<xa-datasource>...</xa-datasource>specification.
Root Cause
One of the following is likely to be the source of the failure:
-
In the driver configuration, a driver class was specified for the
[xa-]datasource-classproperty:<driver name="oracle" module="com.oracle.jdbc"> <datasource-class>oracle.jdbc.driver.OracleDriver</datasource-class> <!-- This is incorrect --> <!-- OR --> <xa-datasource-class>oracle.jdbc.driver.OracleDriver</xa-datasource-class> <!-- This is incorrect --> </driver> -
In the (non-XA)
datasourceconfiguration, a driver class was specified for thedatasource-classproperty:<datasource-class>oracle.jdbc.driver.OracleDriver</datasource-class> <!-- This is incorrect --> -
In the
xa-datasourceconfiguration, a driver class was specified for thexa-datasource-classproperty:<xa-datasource-class>oracle.jdbc.driver.OracleDriver</xa-datasource-class> <!-- This is incorrect -->
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.