Adding the JDBC Driver


As mature as relational database technologies are, there is still no standard network protocol for accessing them. JDBC saves us from needing to couple our application to proprietary database APIs by letting us hide the proprietary access protocols behind a nice, standard Java interface. To do that, we will need to provide the JDBC driver code to JBoss.

How do I do that?

The first step is to locate the JDBC driver code. The MySQL driver is called MySql Connector/J. The current production version is Connector/J 3.0, which you can download from http://www.mysql.com/products/connector/j/. The driver JAR file is located inside of the archive you downloaded. For the 3.0.16 version, which is current as of this writing, the name of the file is mysql-connector-java-3.0.16-ga-bin.jar.

Once you've located the JDBC JAR file, you will need to copy it to the lib directory of your JBoss server configuration. As you recall, we are working from the default configuration, so that would be server/default/lib.


Note: The decision to make a library JAR file static or hot deployable is always tough.

What just happened?

Well, nothing happened. We've talked about the hot deploy feature of JBoss, but that works only for items placed in the deploy directory. The lib directory works differently. JARs in the lib directory are added to the topmost JBoss classloader and cannot be redeployed or removed. The lib directory is ignored after JBoss starts up. The only way to get JBoss to notice the new JDBC driver is to restart the server, so you'll need to shut down and restart your JBoss instance after installing the driver.

How do we know that JBoss has found our driver? We would hope that JBoss would give us some kind of warning or error when we try to use the database connection, but that is a very delayed form of feedback when we haven't yet seen how to use the new database.

There is a solution, though. We'll use the JMX Console to check on our class. Open the JMX Console in your web browser (http://localhost:8080/jmx-console). Near the top of the screen you will see the JMImplementation domain, which has three managed beans (MBeans), as shown here:

     JMImplementation       • name=Default,service=LoaderRepository       • type=MBeanRegistry       • type=MBeanServerDelegate 

Selecting the Loader repository brings you to the management interface for the loader repository (see Figure 4-1).

Developer's Notebook 4-1. The loader repository MBean


We can use the displayClassInfo operation, which is shown in Figure 4-1, to find out what JBoss knows about the class. The operation takes a Java class name as input, so enter the name of our MySQL driver (com.mysql.jdbc.Driver) and click the Invoke button. The MBean will respond with the following text:


Note: You can use this to check whether any class is visible in JBoss.
     com.mysql.jdbc.Driver Information     Not loaded in repository cache     ### Instance0 found in UCL: org.jboss.mx.loading.UnifiedClassLoader3@f8d6a6     ... 

We haven't used the class yet, so JBoss has not yet loaded the class into memory. However, it can see that the driver class is visible in one of JBoss's classloaders. If JBoss had already loaded the class, we would have gotten much more information back from the displayClassInfo operation, as shown in the following listing:

     com.mysql.jdbc.Driver Information     Repository cache version:     com.mysql.jdbc.Driver(85d49e).ClassLoader=org.jboss.mx.loading.UnifiedClassLoader3@     f8d6a6{ url=file:/private/tmp/jboss-4.0.2/server/default/tmp/deploy/tmp14828jboss-     service.xml ,addedOrder=2}     ..org.jboss.mx.loading.UnifiedClassLoader3@f8d6a6{ url=file:/private/tmp/jboss-     4.0.2/server/default/tmp/deploy/tmp14828jboss-service.xml ,addedOrder=2}     ....file:/private/tmp/jboss-4.0.2/server/default/tmp/deploy/tmp14828jboss-service.xml     ....file:/private/tmp/jboss-4.0.2/server/default/lib/activation.jar     ....file:/private/tmp/jboss-4.0.2/server/default/lib/avalon-framework.jar     ....file:/private/tmp/jboss-4.0.2/server/default/lib/bcel.jar        ...     ++++CodeSource:       (file:/private/tmp/jboss-4.0.2/server/default/lib/mysql-connector-       java-3.0.16-ga-bin.jar )     Implemented Interfaces:     ### Instance0 found in UCL: org.jboss.mx.loading.UnifiedClassLoader3@f8d6a6 


Note: Class loading can be tricky, but JBoss provides all the tools you need to understand what is going on.

This tells us that we have indeed loaded the MySQL driver into memory, and JBoss is using the version from the JAR file we put in server/default/lib. This might seem like a lot more detail than we want, but when you have different versions of JARs in different packages and are trying to understand exactly how your classes are being loaded, the extra information is invaluable. But for now, we just want to know that JBoss can locate our driver class.

What about...

...placing the JDBC driver in the deploy directory?

It certainly seems harsh that we had to restart JBoss to make it see our little JDBC driver. Why can't we just put the JDBC driver in the deploy directory and let it be hot deployed? Well, we could have. And it would work almost the same. However, it's generally accepted that it's better to put fundamental service code in the lib directory. JAR files in the deploy directory will be redeployed if you accidentally touch them, through an automated build script, for example. That can create havoc if several applications share the JDBC driver.


Note: Wouldn't it be nice if relational databases had a standard network protocol so that we didn't have to mess with drivers?

We really just don't think of a JDBC driver as being an application component. It's generally thought of as system-level code and is placed with the other system-level JAR files in the lib directory. However, if it's more convenient, feel free to put the JDBC driver along with your application code in the deploy directory.



JBoss. A Developer's Notebook
JBoss: A Developers Notebook
ISBN: 0596100078
EAN: 2147483647
Year: 2003
Pages: 106

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net