Friday, June 01, 2007

Why We Have DriverManager.registerDriver()

When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager”-the Driver interface of JDBC API has made it very clear about its intentions. So the user now can register the Driver by calling Class.forName(the driver name).This is a simplest case of dynamic class loading and is used extensively in implementing Enterprise systems. For others, there are two choices: either call Class.forName() or call DriverManager.registerDriver().

Let us do the latter.

Driver driver=new com.mysql.jdbc.Driver();

DriverManager.registerDriver(driver);

The point which I am curious about is that now the DriverManager has two Driver instances registered with it and every time we try to get a connection, it iterates over the list containing these two drivers.Well! Our intention was to have a single driver instance and we end up having two. Why we have two driver instances is simple as in Driver class we have a static block as

static

{

try

{

DriverManager.registerDriver(new Driver());

}

catch(SQLException E)

{

throw new RuntimeException("Can't register driver!");

}

}

So when we call new Driver(), first registration happens and the next is performed by us.

Well! After this long story, the only thing which I am curious about is “Why we have a register method in DriverManager class where we can do our job with Class.forName() or just calling new Driver()”.

Since I have never used DriverManager.registerDriver() nor I have seen somebody using it.I would appreciate if someone comes up with its usage.






2 comments:

A said...

From the javadocs - "When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager."
How would it register the instance with the DriverManager if the DM does not expose a public API? (which is what registerDriver is).
So DM.registerDriver is for use by drivers.
Class.forName is for use by applications/users of drivers.

Arijit said...

Well!Can we go for the following workaround
-the Driver class registers itself in the default constructor,say,"DriverManager.registerDriver(this)" and if the user again try to register the same instance then the DriverManager checks for duplicates.Like

Driver driver = (Driver) Class.forName("com.pramati.TestClass").newInstance();

DriverManager.registerDriver(driver);

And in the DriverManager.registerDriver() method we have

if (!initialized) {
initialize();
}

boolean exists=false;
for (int i = 0; i < drivers.size(); i++) {
DriverInfo di = (DriverInfo)drivers.elementAt(i);

if(di.driver.equals(driver))
exists=true;

}

if(!exists)
{
DriverInfo di = new DriverInfo();
di.driver = driver;
di.driverClass = driver.getClass();
di.driverClassName = di.driverClass.getName();
drivers.addElement(di);
println("registerDriver: " + di);

}

Is it feasible? Or shall the docs explicitly say that registerDriver() should not be used by application developer?..