Tuesday, April 24, 2012

What will Class.forName do while loading drivers


Class.forName() returns a type of java.lang.class
It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.
Supports Dynamic loading of Java classes at runtime. It provides the basis for application servers. ForName() method of java.lang.Class.

One of Java’s strongest features is its ability to dynamically load code given the name of the class to load, without having to know the actual classname until runtime. This allows Java developers to build flexible, dynamic systems that can grow and change without requiring complete recompilation.
In a program named DynamicLoader have line like …
Class toRun = Class.forName(args[0]);

This means we can run any compiled Java class through DynamicLoader without having to change DynamicLoader’s code.

java DynamicLoader Echo ONE TWO THREE

The classloader attempts to (i) Locate (ii) load and (iii) link the Driver class in the "org.gjt.mm.mysql" package and if successful, the static initializer is run.

To answer the question I started with the JavaDoc for the Class.forName() method. According to the documentation, the method:

... attempts to locate, load, and link the class or interface

I wasn't perfectly clear on what "locate, load, and link" meant, so I did a little digging through the Java Language Specification. According to chapter 12 of the JLS: 

Loading refers to the process of finding the binary form of a class or interface type with a particular name, perhaps by computing it on the fly, but more typically by retrieving a binary representation previously computed from source code by a compiler, and constructing, from that binary form, a Class object to represent the class or interface.

Next, again according to the JLS, it must be transformed from it's binary representation to something the Java virtual machine can use, this process is called linking. Finally, the class is initialized, which is the process that executes the static initializer and the initializers for static fields declared in the class. 

So then back to the original problem, when Class.forName() is called with an argument like this:

Class.forName("org.gjt.mm.mysql.Driver");

The classloader attempts to (i) load and (ii) link the Driver class in the "org.gjt.mm.mysql" package and if successful, the static initializer is run. The MySQL Driver (download the source code) static initializer looks like this:

static {
  try {
    java.sql.DriverManager.registerDriver(new Driver());
  } catch (SQLException E) {
    throw new RuntimeException("Can't register driver!");
  }
}

So it calls a static method in the java.sql.DriverManager class which apparently registers a copy of itself when it loads.

So now I understand the where and the how, what about why? To understand the why you have to look at the next line in the initial code example:

Connection con = DriverManager.getConnection(url,  "myLogin", "myPassword");
Conn.setAutoCommit(false);


No comments: