Sunday, July 4, 2010

what does the Class.forname() do?

Well the important thing about forName() method of Class is that it basically loads the driver.
Now, what do we mean by loading the driver?

It basically takes the class name passed as the string and it executes the static blocks of that particular class.
Now these static block have the code that is needed for making Driver Manager aware of the fact that there is one more driver been added to the list of the available drivers.Hence when are request for that particular driver is made, it is available for the Driver Manager.

Whats interested to notice is the fact that forName() does not create an instance of the class passed as string parameter.It basically executes the static part of a class.

What's the difference between web server and application server

When it comes the web servers they can process HTTP requests and the main job of these servers is to just send the response for the request received from web browser in the static form (may be Html,images etc) or send a redirect or delegate the response to other program like CGI, JSP(JavaServer Pages) ,server side JS.

Hence Web servers do not have the capability of the executing any business logic  Its job is to just get the request serve the request and send the response back.This results in lot of draw backs, which includes application scalability problem,session management


Whereas in Application servers, you can put in business logic for which would be executed by the server for the use of client program.This is mainly done by means of whats called EJB(Enterprise Java Beans).
Also its interesting that Application servers are the super set of the Web servers and hence Application server is capable of doing what web servers do(only allocating resources).
Its important to note that Application server clients can also be GUI running on PC and other Application server.

Thursday, January 21, 2010

Implicit objects in JSP

Well when it comes to Java Server Pages(JSP),the most important concept is "implicit objects".
What are implicit object basically? These are the objects that are created by JSP container (server) and you do not need to explicitly create these objects.
There are basically 9 implicit object as listed bellow(along with the corresponding API)
API implicit object

JspWriter ==> out
HttpServletRequest ==> request
HttpServletResponse ==>response
HttpSession ==>session
ServletContext ==> application
ServletConfig ==> config
Throwable ==> exception
PageContext ==> pageContext
Object ==> page

Well looking at the list of API we make out that these are the objects which basically give JSP the servletness features.Well if you are good at servlet than JPS is not that hard and so the "implicit objects".


Wednesday, January 20, 2010

An Example for "dirty checking" in Hibernate

As I have written about the features of hibernate in the previous post.One of them was dirty checking by hibernate.
Now what does dirty checking actually means?To see this practically lets look at the following class with name Computer.

public class Computer
{
private String name;
private Double prize;
public Computer()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Double getPrize()
{
return prize;
}
public void setPrize(Double prize)
{
this.prize = prize;
}
}
Here I have prize and name as class attributes and I have setter and getter for them.Also I make the name attribute as the primary key in the .hbm.xml file.
Now lets have a class(say Manager) which deals with the retrieval of the row of table Computer,which looks as given bellow


public class Manager
{
public static void main(String[] args)
{
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Computer c = (Computer)session.load(Computer.class, new String("Mac"));
System.out.println(c.getName());
System.out.println(c.getPrize());
c.setPrize(111111);=========>imp. step
session.getTransaction().commit();
session.flush();
session.close();
}
}
Now here when I make the prize of the computer as 111111 in the imp. step, and after the execution of Manager class gets over and I go to the DB and see that the value of the prize has been changed to 111111.
Now the point to be noted is that we changed prize to a new value and did not do a explicit save after changing the value(by calling session.save(c)),but still the changes are made.This auto-updating of DB with reference to the change in the object is called "dirty checking" in terms of Hibernate.

Tuesday, October 13, 2009

Why to use hibernate when other persistence mechanism are there??

Well there are many reasons for hibernate to be used in place of others.

I list some of the most important reasons.

1:)Hibernate provides the Object Relation Mapping(ORM):
Well it has always been a concern for the java developers,they wanted a tool that can provide a proper ORM.Through ORM,the classes(POJO) are directly mapped to the tables and the object state is persisted as the row in that table.

2:)Hibernate provides Transparent and Automated persistence:
Transparent means that hibernate allows the class (that is to be persisted) to be free from any special implementation.Meaning that you don't have to implement any interface or extends to any class.Hence the developers don't have to consider anything about the persistence.

Automated is something that allows the programmers to be free from the low level database implements.Hence developers don't have to write low level queries,they are generated by hibernate(it uses its own query language called HQL).

3:)Hibernate supports Dirty Checking:
This makes sure that any changes that are made to the object will also be reflected in the database.Hence you don't have to again explicitly save the object.

4:)Hibernate support Fine-grained domain models:
This is the most important requirement for rich domain model.This basically allows you to have more classes than the tables.

Example in a design for a User entity we may have address,street no,city etc. as attributes,but wait there is a problem with this:what if a User has a home address,office address.Well you can have soo maany columns for each of them.

But in hibernate you can have a separate class called Address with all of the above as attributes.This basically reduces the time taken to execute the queries.

5:)Hibernate also supports the dynamic manipulation of meta-data:
Using this you can update the database schema even at the run time.

6:)Hibernate offers platform independence:
With this you can even work on legacy systems with out any problems.

7:)Hibernate supports polymorphic queries:
Well this feature of Hibernate is quite unique.This basically allows to retrieve the DB row and store it in super class reference.

Well the list continues.I must say that its really worth using Hibernate in application development.But its always better to use it in big application.

Friday, August 7, 2009

knowing class file content using javap command....


Well as we know that there is lots of discussion done when it comes to memory allocation for the String class Objects.As we have the concept of "constant pool" and "non-constant pool",the allocation of memory is quit different and really gets confusing some times.

Even in the scenarios like using String class and StringBuffer class,we all know that its better to use the StringBuffer object compared to that of String class as we can append some string despite of concatinating it,we do not actually know why its better but we implement it that way only.

We can obtain the best possible solution to these problems if we can see the bytecode generated after translation of the .java file to .class file.

Well java is not behind even in this aspect of it.Sun has provided a great tool called "javap" command.

Now what javap command does is,it takes the .class file name and prints the information about the class:access specifers,method signature,classes used with fully qualified package...

Also if you use option c with this command we get a different representation....
what it does??well it does not decompile the bytecode but it disassembles the code and gives the bytecode instruction specified by the java virtual mechine.....

Well take a look at this example....String class objects for constant and non constant pool.....

package com.lara;

public class Example
{
public static void main(String[] args)
{
}
void usingStringConstantPool()
{
String s = "java";
s = s+"geek";
System.out.println(s);
}
void usingStringNonConstantPool()
{
String s1 = new String("java");
s1 = s1+"geek";
System.out.println(s1);
}
}

now see this,what happens next...

E:\classFiles\com\lara>javap -c Example
Compiled from "Example.java"
public class com.lara.Example extends java.lang.Object{
public com.lara.Example();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public static void main(java.lang.String[]);
Code:
0: return

void usingStringConstantPool();
Code:
0: ldc #2; //String java
2: astore_1
3: new #3; //class java/lang/StringBuilder
6: dup
7: invokespecial #4; //Method java/lang/StringBuilder."":()V
10: aload_1
11: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
14: ldc #6; //String geek
16: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
19: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
22: astore_1
23: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
26: aload_1
27: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
30: return

void usingStringNonConstantPool();
Code:
0: new #10; //class java/lang/String
3: dup
4: ldc #2; //String java
6: invokespecial #11; //Method java/lang/String."":(Ljava/lang/String;)V
9: astore_1
10: new #3; //class java/lang/StringBuilder
13: dup
14: invokespecial #4; //Method java/lang/StringBuilder."":()V
17: aload_1
18: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: ldc #6; //String geek
23: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
26: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
29: astore_1
30: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
33: aload_1
34: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
37: return

}

now the stuff that you see is more of the JVM specified instructions,so we won't go to that part but we can really make out from the out put that there is new operator being used by the second function twice hence 2 objects are created whereas in first case there is only one object created....
and that's perfectly fine with the theory about these....

But what a command,we can really have own proof of what exactly happens.....

There are other options also available..you can go through the API....one more interesting option is l which prints the local variable and out line table...


Friday, July 31, 2009

Is it that we should always use preparedStatement???(statement vs preparedStatement)

Well when it comes to the competition between preparedStatement and the statement object in JDBC,most of the time its preparedStatement that wins the game....
Reason being simple and not suppressing....

1:)Performance
2:)Efficiency

Actually when we get the statement object from connection object in JDBC there are three things that are done on DB side "FOR EVERY RUN OF THE JAVA PROGRAM"...
#1:The database query is first compiled
#2:there is a query plan that is generated by the database manager
#3:than at last we have the query being executed...

Now if we consider the case of the preparedStatement,than at the first run or execution of the program there is all the three steps that are carried out..but for the subsequent running of the program there will only be the last step that is carried out...
That's because the compiled query and the execution plan is already there with the DB...

Well than whats the use of having the statement object????
the prepapredStatement is considered to the static object...as there is only one type of query that you can possible execute with it....

Whereas in case of the statement object it considered to be the dynamic object as you can execute multiple query from one statement object.....