Default style (Cherry Eve). Switch styles (Capricorn). Atom Feed Calendar
http://blogs.sun.com/blogAmit/date/20060220 Monday February 20, 2006

JDBC Driver Loading with Mustang


We have simplified the way to get a JDBC connection from Mustang. This is part of the Ease of Development features for which we laid the foundation in Tiger and are being built into the java platform.


So what has changed ?
Pre Mustang, to get a JDBC connection, in stand alone mode, we do



Class.forName("com.myorg.jdbc.jdbcDriverImpl");
Connection con = DriverManager.getConnection(url, user, pass);


Post Mustang, to get a JDBC connection, in stand alone mode, it is

If a JDBC driver is packaged as a service, you can simply say



// commented Class.forName() below
// Class.forName("com.myorg.jdbc.jdbcDriverImpl");

Connection con = DriverManager.getConnection(url, user, pass);


What is meant by JDBC driver being packaged as a service ?
The Service Provider mechanism is defined according the the JAR Specification. So JDBC driver vendors need to packages their drivers as a service. The DriverManager code will look for implementations of java.sql.Driver in classpath and do Class.forName() implicitly.

How does it help a developer ?
(1) Less clutter in code.

(2) The developer no longer needs to know the class which implements java.sql.Driver interface from the JDBC driver jar.


Can the developer simply ignore calling Class.forName() from Mustang ?
No, they can't. If your JDBC driver vendor supplies the driver exposing it as a service, only and only then, you can ignore calling Class.forName(). Else the semantics remain same as they were with pre JDBC 4.0.


Will the existing applications still run with this change ?
Yes, very much. This change ensures backward compatibility i.e. applications which get ported to Mustang will still run. New applications can be written to reduce clutter in the code. The idea is to move away from explicitly doing a Class.forName() and that eventually all driver vendors package their JDBC drivers exposed as a service.


How will things change in the Java EE world ?
Nothing changes in the Java EE world. This driver loading is primarily for non managed scenario or stand alone applications. The way you get a connection from a DataSource stays as it is.


What's the advantage to the driver vendor ?
One advantage from driver vendor's perspective is that the vendor can change the name of the class which implements java.sql.Driver interface without worrying too much about a developer's application. But that's a long way to go, after this gets adopted as the default way in stand alone applications to get a JDBC connection.


All these changes(see Lance Andersen's blog, Spec Lead JDBC 4.0) are being done as part of JDBC 4.0 which is being delivered as part of Mustang. I will write more on some of the other features soon...

Comments:

Nice to see you here! Welcome to blogging!!

Posted by A. Sundararajan on February 20, 2006 at 04:18 PM IST #

Blog is simple and good. All the best for your upcoming blogs... Found simple mistakes in the following lines: 1. according 'the' the JAR Specification (should be 'to' the) 2. So JDBC driver vendors 'need to packages' their drivers (should be 'need to package') 3. The developer no longer 'needs' to know the class (should be 'need') Thanks for sharing my comments. Thanks Jothir

Posted by jothir on February 21, 2006 at 09:18 AM IST #

Amit, Could you please explain what would happen if multiple database drievers packaged? How does an application program use a specific driver? regards sankar

Posted by sankara on February 21, 2006 at 11:42 AM IST #

Nice write up. Good start....hope this is one of many to come :-)

Posted by Shreyas on February 21, 2006 at 12:00 PM IST #

Sankar, When two or more drivers are exposed as a service in the classpath, the DriverManager.getConnection will try to get a connection which is first in the classpath. If it cannot get, it will keep on iterating over the classpath until it gets a connection.

Posted by Amit Handa on February 21, 2006 at 12:20 PM IST #

Thanks jothir! you can blame the typos on my typing and to get the blog out...

Posted by Amit Handa on February 21, 2006 at 12:22 PM IST #

To add more to what I said above, when multple drivers are there, DriverManager.getConnection() will get a Connection based on the url passed. But if two drivers versions are there in classpath with the url which can give a JDBC Connection, then the jar which comes first in the classpath will be picked. I hope it is more clear now...

Posted by Amit Handa on February 21, 2006 at 02:03 PM IST #

Handa Saab !!! Nice to see your work on JDBC.

Posted by Surendra Prasad on February 22, 2006 at 04:29 PM IST #

what happens with java webstart where Class.forName() doesn't work cause you need to use the classloader

Posted by alex on February 24, 2006 at 02:07 AM IST #

alex, we are covered for that, internally we are using java.util.Service(introduced in JDK 6 aka Mustang) which will do a Thread.currentThread. Do you have a case where it is not working with java web start ? if so please do let us know.

Posted by Amit Handa on February 25, 2006 at 11:07 AM IST #

Post a Comment:
Comments are closed for this entry.