Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of Class.forName()

 
Murali Mohan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any advantage in using Class.forName() over DriverManager.registerDriver()?
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me state up front that I'm biased in favor of using "Class.forName()". But either way works equally well. In the JavaDoc for the java.sql.Driver class it states:

When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a driver by calling
Class.forName("foo.bah.Driver")

Most driver classes accomplish this by having a "static { ... }" block of code that creates an instance and calls "DriverManger.registerDriver()" on that instance.
So if you use "DriverManager.registerDriver(new foo.bar.Driver())" in your code here's what happens:
  • the "foo.bar.Driver" class is loaded
  • a new instance of "foo.bar.Driver" is created in the "static { ... }" initializer of the "someDriver" class
  • that new instance is registered with DriverManager
  • your code creates a second instance of "foo.bar.Driver"
  • the second instance is registered with DriverManager


  • The upshot is that TWO instances of "foo.bar.Driver" are created. If you use the "Class.forName()" method only ONE instance of "foo.bar.Driver" is created.
    [ December 05, 2003: Message edited by: Wayne L Johnson ]
     
    Murali Mohan
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It seems only one instance of driver is created ......Can u pls elaborate it ?
     
    Pradeep bhatt
    Ranch Hand
    Posts: 8933
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It seems only one instance of driver is created

    Yes it true. Why do you want many instances?
     
    Murali Mohan
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In any case one one instance of driver then what is the use of class.forName() over other method ?
     
    David O'Meara
    Rancher
    Posts: 13459
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You should always use Class.forName and allow the driver to register itself and should never call DriverManager.registerDriver() directly.
    Otherwise, the driver registers itself when the class is loaded, and then gets registered again when you call the DriverManager. You can't stop this from occuring, since the initial load happens as the Class loads in a static code block.
    Chances are this won't cause you any problems, but it will mean DriverManager.deregisterDriver() won't work correctly. If you check the DrioverManager source, you will cause the Driver to be registered twice, but it will only be unloded once - ie it still exists!
    Dave
     
    Murali Mohan
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What I understood from above is with "new driver()" method only driver can be loaded and registered. With Driver.register()method it will be registered second time.
    Then why don't we avoid Driver.register() method()???
    Thanks,
    Murali
     
    Jason Steele
    Ranch Hand
    Posts: 100
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Class.forName() (the preferred method) will ensure that there is always an instance of the driver registered. One a driver is registered, there is no reason to re-register. The driver will always be the same.
     
    Amit Kaushik
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dear Murli,
    Please read this and you would get to know how the whole thing works.
    http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/drivermanager.html
    Cheers
    Amit Kaushik
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic