Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Janelle Goldsmith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to be able to use it. I am not sure what the question is here.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic