wood burning stoves 2.0*
The moose likes JDBC and the fly likes Why should an application be able to see the drivers loaded by the same class loader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Why should an application be able to see the drivers loaded by the same class loader " Watch "Why should an application be able to see the drivers loaded by the same class loader " New topic
Author

Why should an application be able to see the drivers loaded by the same class loader

Janelle Goldsmith
Greenhorn

Joined: Mar 20, 2003
Posts: 12
Hi All,
Got the following information from a book -
"The DriverManager associates each registered JDBC driver with the class loader of the class which registered the driver, so that the JDBC drivers available to an application are only the ones which have been loaded by the same class loader."
Why should an application be able to see the drivers loaded by the same class loader ?
Thanks
Murali Nanchala
Ranch Hand

Joined: Mar 14, 2001
Posts: 74
In order to be able to use it. I am not sure what the question is here.


"It is almost always answered in the API" - Murali Nanchala
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I'm not sure either, but if I understand what you're saying, then you have it backwards.
A ClassLoader will only be associated with a single application, but an application can have multiple ClassLoaders.
It is also possible to load a single Class multiple times in an application, depending on how many ClassLoaders you have and how they are configured.
Finally, this allows a single Class to have the same database Driver multiple times in a single application. I can't quite see the point off the top of my head, but I'm sure there must be a reason.
(As a complete guess, it might have to do with the fact that the DriverManager acts as a Singleton Pattern, but this is only supported by its ClassLoader, therefore the registration of Drivers must also be supported by ClassLoaders. Otherwise it might be possible for two DriverManagers to exist, and have a Class expect the Driver to be loaded even though the Driver was loaded against a DriverManager on a different ClassLoader. Or something)
Dave
Janelle Goldsmith
Greenhorn

Joined: Mar 20, 2003
Posts: 12
Hi David,
Your reply created a basic question in my mind -
If an application has multiple class loaders, is it possible to have multiple instances of a static class ?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It is more likely to occur as a result of a distributed application or an application running on an application server (which use multiple ClassLoader tricks), but sure, I can't see why not.
The usual behaviour of a ClassLoader is to test to see if its 'parent' has loaded a class before trying to load it again. However, two ClassLoaders with the same parent can both load the same class if the parent hasn't loaded it.
Also, Application Servers short circuit the the 'load from parent' rule so that in some cases it doesn't happen. This allows the app servers to use ClassLoaders as the boundaries between multiple applications running on them.
And we're way off topic
 
Don't get me started about those stupid light bulbs.
 
subject: Why should an application be able to see the drivers loaded by the same class loader
 
Similar Threads
How my program runs without registering driver?
ways in doing database connection?
Selection of Driver for Connection
Regarding Class loading
Trying again: SQLException: No suitable driver