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

loading driver

 
kooskuntla venkat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference with Class.forname and DriverManager
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kooskuntla venkat,

The following line of code
Class.forName("MyDriver");
will load the Driver with name MyDriver and also registers it with DriverManager by calling DriverManager.registerDriver().There is no need for you to register that driver explicitly by calling DriverManager.registerDriver() because Class.forName is doing both the things for you.

Regards Sujith
 
kooskuntla venkat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sujith Kanaparthi:
Hi kooskuntla venkat,

The following line of code
Class.forName("MyDriver");
will load the Driver with name MyDriver and also registers it with DriverManager by calling DriverManager.registerDriver().There is no need for you to register that driver explicitly by calling DriverManager.registerDriver() because Class.forName is doing both the things for you.

Regards Sujith
 
kooskuntla venkat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello sujith
is there any memory management issue arises while loading driver with class.forname and drivermanager.registerdriver
from venkat
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use DriverManager explicitly, you will register the Driver twice and there will be two instances held by the DriverManager. Ithe grand scheme of tings the memory is negligable (unless you keep registering it over and over and over again) so you should avid doing it, but otherwise don't worry about it.

Dave
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ithe grand scheme of tings the memory is negligable (unless you keep registering it over and over and over again)

Hi David,

I hope Every Driver class has a static block of that creates a new Instance of the Diver and registers it with DriverManager.Since it is a static block that is being executed, even if try to register it once again the code is not going to execute once again.
David please comment on this .
Regards
Sujith
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call Class.forName(...) as many times as you like and the Class will only be loaded once, and as a side effect the Class will only register itself with the DriverManager once.

BUT (and this was true the last time I checked the DriverManager source, but feel free to see if they have fixed it) calling DriverManager.registerDriver(...) will cause the Driver to be registered every time the line is called.

So the following code:

will register the Driver twice the first time it is called, and once each time it is called after that. I'm pretty sure this is not what you intend.
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
I Thank you for the clarification that you have provided on what happens when DriverManager.registerDriver(..) is called.
Regards
Sujith
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic