• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Use of Class.forName()

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any advantage in using Class.forName() over DriverManager.registerDriver()?
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
     
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It seems only one instance of driver is created ......Can u pls elaborate it ?
     
    Ranch Hand
    Posts: 8953
    Firefox Browser Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In any case one one instance of driver then what is the use of class.forName() over other method ?
     
    Rancher
    Posts: 13459
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Ranch Hand
    Posts: 100
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
    Clean our rivers and oceans from home
    https://www.kickstarter.com/projects/paulwheaton/willow-feeders
    reply
      Bookmark Topic Watch Topic
    • New Topic