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


How to count number of objects for a class in JVM

Simple ..


public class LikeThis {
  private static int numberOfObjectsCreated = 0;
 
   public  LikeThis() {
    numberOfObjectsCreated++;
    // other constructor business  
  }
}

what is difference between www and www2

We all know that “WWW” stands for World Wide Web, which is a kind of prefix before normal domain name. Web was created around 1990 in Geneva, Switzerland. But what is WWW1 or WWW2? 


You must noticed www1 or www2 once in a while when you are visiting a website. WWW1, WWW2 or even WWW3 is nothing but a mirror of the original web server which is typical WWW. Many websites like government, banks even major search engines like Google(www1.google.com or www2.google.com) and Yahoo(www1.yahoo.com or www2.yahoo.com)! uses www1 and www2. The main purpose of this techniques is to reduce server load. Sometimes the original server need to be updated or modified but major websites like .gov, .edu, google.com, yahoo.com cannot just shut their main server down for hours to update their system. Therefore, they need to use www1 or www2 duplication of their mainframe server. 


Sometimes this different servers used for the serving users based on their geographical location. Suppose, if someone typehttp://www.google.com form US, they might be taken to the original google server which means http://www.google.com but if someone type http://www.google.com from Africa, it might take them to www2.google.com or www1.google.com. 


I was surprised that not a lot of information showed up in Google or Yahoo when i was searching for it. If you know more about WWW2 or WWW1 please feel free to share with everyone here by adding your comment at the bottom. I was wondering if there is any security issue behind this mirror web server policy? 



http://en.wikipedia.org/wiki/WWW2