This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes JDBC and the fly likes DriverManager.getDrivers() Basic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "DriverManager.getDrivers() Basic question" Watch "DriverManager.getDrivers() Basic question" New topic
Author

DriverManager.getDrivers() Basic question

L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi,
What exactly happens when i say Class.forName("MyDriverClass") ?
I have two sample programs in one I load the driver and in other I try to get the drivers loded using DriverManager.getDrivers(). But In the second program it doesn't return the driver.
But I I retrieve the driver in first progrma then it lists the driver which is loaded using Class.forName.
I also tried to load DriverManager.registerDriver(new MYDriverClass()). This also does work in the above mentioned scenario.
Here is the code. Thanks for the time.
Example Program to load driver:

Example to test the loaded drivers:


SCJP, SCWCD
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
If you look at the documentation for the java.sql.Driver interface, you'll see the following:

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")

From what I've seen, the driver classes (these are the classes that implement "java.sql.Driver") will include a "static { ... }" block that instantiates the driver and registers it with the "DriverManager" class.
There are various ways of registering drivers, but if you're doing it programmatically, you should always use "Class.forName(...)".
[ February 18, 2004: Message edited by: Wayne L Johnson ]
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi..
I agree with your comments. But what i need to know is in my first program i initialised the driver and since its a static one, it should be present in the JVM's Class Loader. So when I run my second program it should return me the loaded driver. Isn't it ?
Cheers.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
I guess I misunderstood your initial question. Exactly how are you running the tests? Since both of the classes are "public" I must assume that they each reside in separate source files, "Exampe1.java" and "Example2.java".
If you are testing by executing each in turn:

Then the second application one won't find the driver loaded by the first, which is what you are seeing.
When you do "java Example2", the JVM is initialized and starts up, and the "main(...)" method of class "Example2" is invoked, the result that the Oracle driver is loaded and registered. However once the main method exits there are no more active threads and so the JVM exits. Everything you did goes away, and all of your objects are either explicitly garbage collected, or recovered by the OS.
If you then do "java Example1", the JVM is once again initialized and starts up again, but nothing that you did before is visible.
You need to call the two methods from within the same JVM. You could invoke the "Example2.main(...)" from within the Example1 "main(...)" method, which is pretty ugly. Or you could write a third class that calls the two methods in turn (I'd rename the methods to something other than "main") and then start that third class.
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi..
I got it. But Tried this and it didnt work.
I added main method in Example2 and created a endless loop i.e. while(true). So when I run Example2, it loads the driver and the thread is still alive since main method is having endless loop.
Noe I ran the Example1 which lists the drivers. Its not listing the drivers even now.
Does that mean whenever I say "java myprogram" it creates a new JVM thread and the class loader for both of them are different ???
Thanks for the time.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Every time you execute "java myClass" it starts a new JVM. And you can't see anything in another JVM unless you explicitly set up some type of remote communication (i.e., sockets or RMI or http or something).
So if you type "java myClass" and it runs in a loop, and then type "java myOtherClass", you have two JVMs running and nothing that "myClass" has set up--be it static or not--will be visible in "myOtherClass".
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Thanks I didnt know that.
Well if thats the case then what happens when I deploy servlets? Does it run in different JVM instances on the server? Or the container run in the one JVM instance and sevlets reside in container as child threads..?
What about applet? If i have more than one applet on the page does it create more number of instances on the client JRE ?
Thanks for the time.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Servlets run inside of a "container". I tend to use Tomcat, which can be run in a stand-alone mode or part of a larger web server such as Apache. When you start Tomcat it defines a classpath and starts Java--it's own JVM. Since lots of servlets can run inside that JVM they can share resouces, such as connections from a pool, beans, etc.
I believe that when there are multiple applets running in a web page there is still only one JVM, which is why applets can "talk" to each other.
Note that in a distributed model where there are muliple servers handling incoming requests this requires special programming. You either lock the user [each session] into a single server, or you have to synchronize the session state between the servers.
This is a simplistic explanation and perhaps others can add details. But as I mentioned previously, APIs such as sockets, RMI and HTTP allow data--and in some cases Java objects themselves--to be passed between machines and JVMs.
So you could use sockets or RMI to create a server that would dish up database connections, and then a client piece could connect to the server (on a different JVM), get a database connection, and use it to query a database.
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Thanks Johnson..
One more clarification please.
In a distributed environment, is it enough for me to serialize the Java Beans which are used by my web components to make the application diastributable? ( NO EJB in my application )
Do I get any control to choose between the type of distribution where I can say that in my application the session is bound to a specific application server ( Physical Machine ) or the secodn type where the session data gets persisted and transferred to the other machine.
Do I have to worry about the same during design or coding phase of my web application?
Or it is one more feature provided by the application server vendor. ?
Thanks for the time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DriverManager.getDrivers() Basic question