Thursday, October 30, 2008

JVM's Execution Engine - Interpreter + JIT Compiler

//code
int a=1,b=2;


here by using interpreter
let us assume that , it will take 2 sec for doing execution of one statement.(line by line)
so .... it will take 2+2 = 4 sec for the beloved 2 statements.

//code
print a;
print b;
`````````````````````````````````````````````````````````````````
but... in loop or repeated statements execution... like ...

//code
loop 10 times
print a;

here by using interpreter ,
it will take 2 sec for each Iteration.
therefore , 2*10=20 sec

here we come across JIT compiler .
it will compile whole loop statements and store into memory.for this it will take 2 sec.
and for executing loop ,it takes 2 sec.
totally 4 sec . where as by using interpreter we do same ,in 20 sec

--------------------------------------------------------------------------------------------------------

in c,c++ like compiling languages ..
we convert them to intermediate form ... and immediate for execution.
here compiled file is not similar to text file.and it may contain pointer references.
there is a scope for virus attack.

- here ,Java.
we have bytecode as intermediate form ...which is input to JVM's execution engine - Interpreter,here we come across above discussed concepts in,Interpreter + JIT Compiler.

Wednesday, October 29, 2008

JDBC Driver Types




JDBC drivers are divided into four types or levels. The different types of jdbc drivers are:

Type 1: JDBC-ODBC Bridge driver (Bridge)
Type 2: Native-API/partly Java driver (Native)
Type 3: AllJava/Net-protocol driver (Middleware)
Type 4: All Java/Native-protocol driver (Pure)

4 types of jdbc drivers are elaborated in detail as shown below:

Type 1 JDBC Driver - JDBC-ODBC Bridge driver


The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.

Functions

* Translates query obtained by JDBC into corresponding ODBC query, which is then handled by the ODBC driver.
* Sun provides a JDBC-ODBC Bridge driver. sun.jdbc.odbc.JdbcOdbcDriver. This driver is native code and not Java, and is closed source.
* Client -> JDBC Driver -> ODBC Driver -> Database
* There is some overhead associated with the translation work to go from JDBC to ODBC.

Advantage

The JDBC-ODBC Bridge allows access to almost any database, since the database's ODBC drivers are already available.

Disadvantages

1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable.
2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types.
3. The client system requires the ODBC Installation to use the driver.
4. Not good for the Web.

Type 2 JDBC Driver - Native-API/partly Java driver



The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. Some distinctive characteristic of type 2 jdbc drivers are shown below. Example: Oracle will have oracle native api.


Functions
* This type of driver converts JDBC calls into calls to the client API for that database.
* Client -> JDBC Driver -> Vendor Client DB Library -> Database


Advantage

The distinctive characteristic of type 2 jdbc drivers are that they are typically offer better performance than the JDBC-ODBC Bridge as the layers of communication (tiers) are less than that of Type
1 and also it uses Native api which is Database specific.

Disadvantage


1. Native API must be installed in the Client System and hence type 2 drivers cannot be used for the Internet(protocol conversion logic resides with client machine).
2. Like Type 1 drivers, it’s not written in Java Language which forms a portability issue.
3. If we change the Database we have to change the native api as it is specific to a database
4. Mostly obsolete now
5. Usually not thread safe.

Type 3 JDBC Driver - All Java/Net-protocol driver

Type 3 database requests are passed through the network to the middle-tier server. The middle-tier then translates the request to the database. If the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers.


Functions

* Follows a three tier communication approach.
* Can interface to multiple databases - Not vendor specific.
* The JDBC Client driver written in java, communicates with a middleware- net-server using a database independent protocol, and then this net server translates this request into database commands for that database.
* Thus the client driver to middleware communication is database independent.
* Client -> JDBC Driver -> Middleware-Net Server -> Any Database


Advantage

1. This driver is server-based, so there is no need for any vendor database library to be present on client machines.
2. This driver is fully written in Java and hence Portable. It is suitable for the web.
3. There are many opportunities to optimize portability, performance, and scalability.
4. The net protocol can be designed to make the client JDBC driver very small and fast to load.
5. The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced
system administration such as logging and auditing.
6. This driver is very flexible allows access to multiple databases using one driver.
7. They are the most efficient amongst all driver types.

Disadvantage

It requires another server application to install and maintain. Traversing the recordset may take longer, since the data comes through the backend server
(This differs from the type 4 driver in that the protocol conversion logic resides not at the client, but in the middle-tier).

Type 4 JDBC Driver - Native-protocol/all-Java driver



The Type 4 uses java networking libraries to communicate directly with the database server.


Functions

* Type 4 drivers are entirely written in Java that communicate directly with a vendor's database, usually through socket connections. No translation or middleware layers are required, improving performance.
* The driver converts JDBC calls into the vendor-specific database protocol so that client applications can communicate directly with the database server.
* Completely implemented in Java to achieve platform independence.
* e.g include the widely used Oracle thin driver - oracle.jdbc.driver. OracleDriver which connect to jdbc:oracle:thin URL format.
* Client Machine -> Native protocol JDBC Driver -> Database server


Advantage

1. The major benefit of using a type 4 jdbc drivers are that they are completely written in Java to achieve platform independence and eliminate deployment administration issues. It is most suitable for the web.
2. Number of translation layers is very less i.e. type 4 JDBC drivers don't have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good.
3. You don’t need to install special software on the client or server. Further, these drivers can be downloaded dynamically.

Disadvantage

With type 4 drivers, the user needs a different driver for each database.

Monday, October 13, 2008

Top Nine Errors Java Programmers Make

* A .java source file can contain more than one class but it can contain atmost one Top level class (or interface) definition that is public and the name of the source file must be same as that of the public class (or interface).
* Static methods cannot be overridden but can be hidden if they are not final (Read about Overriding vs Hiding here).
* The objects created in the String pool are not subjected to GC until the class is unloaded by the JVM.
* Only methods or code blocks can be marked “synchronized”.
* Local classes cannot access non-final variables.
* -0.0 == 0.0 is true.
* Collection (singular) is an Interface, but Collections (plural) is a helper class.
* continue must be in a loop (e.g., for, do, while). It cannot appear in case constructs.
* Instance initializers are executed only if an object is constructed.

Thursday, October 2, 2008

ArrayIndexOutOfBoundsException in Java



package com.index;

import java.util.ArrayList;
import java.util.List;

public class ListIndexTest {

 /**
  * @param args
  */
 public static void main(String[] args) {

  List<String> list = new ArrayList<String>();
  int indexOf = 0;
  try {
   indexOf = list.indexOf("test");
   System.out.println(list.remove(indexOf));
  } catch (ArrayIndexOutOfBoundsException e) {
   e.printStackTrace();
  }
  try {
   // avoiding exception by condition check.
   indexOf = list.indexOf("test");
   if(indexOf >= 0){
    System.out.println(indexOf + " "+ list.remove(indexOf));
   }else{
    System.out.println("index of test : "+indexOf);
   }
  } catch (ArrayIndexOutOfBoundsException e) {
   e.printStackTrace();
  }
  list.add("test");
  list.add("rest");
  list.add("quest");
  list.add("test");
  indexOf = list.indexOf("test");
  System.out.println(indexOf + " "+ list.remove(indexOf));
  indexOf = list.indexOf("test");
  if(indexOf >= 0){
   System.out.println(indexOf + " "+ list.remove(indexOf));
  }
 }

}


output:

java.lang.ArrayIndexOutOfBoundsException: -1
 at java.util.ArrayList.elementData(ArrayList.java:371)
 at java.util.ArrayList.remove(ArrayList.java:448)
 at com.index.ListIndexTest.main(ListIndexTest.java:17)
index of test : -1
0 test

2 test

Hibernate and JDBC

Following link has very good explanation about hibernate and JDBC as well as it explains difference between hibernate and JDBC