I want to deploy my own jdbc driver in a scoped classloader
I've added my jdbc driver to a scoped deployment but it fails to create the datasource
with all sorts of classloading problems.
JDBC API Flaw
The JDBC api is fundamentally flawed when it comes to classloading.
This is mainly due to java.sql.DriverManager being a global singleton,
but it also has problems due to internal caching.
The caching is due to the hacky way that JDBC handles security,
see the javadoc discussion
on classloaders/applets.
Error Messages
Possible error messages you may see when attempting to deploy a JDBC driver in a scoped classloader.
Error deploying a MySQL driver to JBoss 4.0.3.SP1
org.jboss.resource.JBossResourceException: Apparently wrong driver class specified for URL: class: com.mysql.jdbc.Driver, url: jdbc:mysql://localhost:3306/testdb
Workaround
To make it work properly, you need to make sure that everything is in the same classloader.
That means deploying the JBoss local jdbc rar (deploy/jboss-local-jdbc.rar) and at least
its support classes (lib/jboss-common-jdbc-wrapper.jar) in your scoped deployment.
Full solution
Lobby the JCP to fix the broken java.sql.DriverManager api (but hell is likely to
freeze over first!
Aside
This "bug" also makes it impossible to hot-deploy jdbc drivers that are not in scoped deployments.
You get ClassCastExceptions if you try it, with no way to fix it because you the DriverManager
won't flush its cache until the classloader is garbage collected. i.e. for the main shared
classloader repository of JBoss this is never!
-
Examples
To deploy a MySQL datasource to JBoss 4.0.3.SP1 in a scoped classloader: -
Include the resource adapter archive 'jboss-local-jdbc.rar' to the ear and reference it within the application.xml, e.g.
<module><connector>jboss-local-jdbc.rar</connector></module>
Also add the MySQL driver to the ear and again reference it within the application.xml, e.g.
<module><java>mysql-connector-java.jar</java></module>
Finally add the -ds.xml to the ear and reference it from the jboss-app.xml, e.g.
<module><service>mysql-ds.xml</service></module>
Comments