Thursday, 24 July 2014

JDBC Driver Types

Java Database Connection is a set of classes that help us to connect and communicate with database.
Every vendor of database add the code in classes that implements JDBC interface and you don't need to re-invent the wheel from scratch; you have just call the public method and every particular method of driver know how to react and behave.

Let's consider 
createConnection(); here is the method and every jdbc vendor driver know how to create a connection with database.Oracle vendor driver know how to create connection with oracle database and similarly Microsoft vendor Driver know how to create connection with the database etc.

Note :You have use JDBC interface from java.sql classes, so the type of driver will not have any logic impact in your code, it will only have run time / deployment impact.




Type of JDBC Driver
  
There are 4 JDBC driver tpes/level
Type 1: JDBC-ODBC Bridge driver (Bridge)

Type 2: Native-API/partly Java driver (Native)


Type 3: All Java/Net-protocol driver (Middle ware)


Type 4: All Java/Native-protocol driver (Pure)



Type 1
ODBC (Open database Connectivity) is actually Microsoft protocol to chat fetching and updating  the data of database.JDBC -ODBC bridge driver which really means an ODBC connection wrapped in JDBC clothing.
 In this implemented class in Java makes calls to the code written in Microsoft languages (native), which speaks directly to the database.
 In this you first create DSN (Domain Source Name) in the client machine then it's registered with the JDBC.
This is very first and slow driver type of JDBC relatively than other driver type;because translation or conversion and many step require to reach database data.


 
Type 2 Driver
A type 2 JDBC driver is like a type 1 driver, except the ODBC part is replaced with a native code part instead.Actually the type-2 drivers  use a Java (JDBC) API to call native api and native code work with the database.





Type 3 Driver

Type 3 drivers basically proxy (or relay) the request to another network resource. That typically incurs an extra network hit, but again, that doesn't say much about actual performance.follows a three-tiered approach whereby the JDBC database requests are passed through the network to the middle-tier server. The middle-tier server then translates the request (directly or indirectly) to the database-specific native-connectivity interface to further the request to the database server.

Type 4 Driver
The Type 4 uses Java  libraries to communicate directly with the database server.This driver is fastest among all drivers.The Java program connects directly to the database if type  driver used.




Sunday, 20 July 2014

N Tier Architecture

How do you structure an application to support such operational requirements as maintainability,re-usability, scalability and robustness? The answer lies in using N tier architecture.
N tier architecture means splitting up the system into N tiers, where N is a number from 1 and up. 
1 tier architecture is the same as a single process architecture.
2 tier architecture is the same as a client / server architecture etc.
3 tier architecture is a very common architecture. A 3 tier architecture is typically split into a presentation or GUI tier, an application logic tier, and a data tier. 

2 Tier Architecture
The two-tier architecture is like client server application. The direct communication takes place between client and server. There is no intermediate between client and server.





The above figure shows the architecture of two-tier. 


Three tier Architecture
The architecture in which whole application infrastructure is divided in to three Tier 

  1. Client Tier/ Presentation Tier
  2. Business Intelligence / Processing Tier
  3. Data Tier


Below picture explain the above picture 


Presentation Tier
Presentation tier are use for where client can interact the with the application.In this Tier there are HTML, CSS, JSP are used.Usually user can interact with application using Web browser (Presentation Layer).

Middle / Business Logic Tier

Business tier are the actually tier for programmer, in this business rule and regulation are defined how system respond and how to react when and where.
This tier is for  Servlet, Core Java other technologies are ASP.net or PHP work only.


Data Tier
Database developer , maintainer and operator are working in this tier.In this SQL (Structure Query Language) are been used for to interact with database and database may be Oracle, SQL Server or MySQL.

Looking on above working strategies , we have realized that work is divided into module and specialized person.Everyone know how to work and perform. If any tier may get some error then only particular tier will be solved , not to check other tier.This give us Highly cohesion and loosely coupled system.
Let suppose if you have desktop system, and your keyboard not working properly then you check only your keyboard not your monitor or mouse.




Object Serialization


Object serialization is a process of converting an object instance into a sequence of bytes (which may be binary).Serialized object is an object represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.After a serialized object has been written into a file, it can be read from the file and deserialized—that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
It is very useful when you want to transmit one object data across the network, for instance from one JVM to another.



In Java ; you can achieve Object Serialization by implement Serializable.
Here is the good convention for to make object serialization
  • First implement the Serializable interface 
  • Your class like POJO (Plain Old Java Object)


Code Example
Employee.java
public class Employee implements Serializable {

        private int empId;
        private String name;

        public int getEmpId() {
            return empId;
        }

        public String getName() {
            return name;
        }

        public void setEmpId(int empId) {
            this.empId = empId;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "EMployee id : " + empId + "  \nEmployee Name : " + name;
        }
}
Another Main public class
pubic class Main{
        public static void main(String[] args) throws FileNotFoundException, IOException,   ClassNotFoundException {

            String filename = "data.txt";
            Employee e = new Employee();
            e.setEmpId(101);
            e.setName("Yasir Shabbir");
        enter code here
            FileOutputStream fos = null;
            ObjectOutputStream out = null;

            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(e);

            out.close();


            // Now to read the object from file
            // save the object to file
            FileInputStream fis = null;
            ObjectInputStream in = null;

            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            e = (Employee) in.readObject();
            in.close();

            System.out.println(e.toString());
        }
    }
}
  

Saturday, 19 July 2014

THREAD

Actually Thread is an Operating System concept, this provide efficient performance through "Parallelism or Concurrency". Parallelism /Concurrency meaning doing many work at the same time without waiting for completion of one another. Thread give us multitasking.



Thread provide mechanism  to perform another work without waiting for completion to another work. 
Thread are also called "Light weight Process". Each Thread shall reside in the Process and no one Thread are outside the process. Every Thread have their own Stack memory, heap memory,register  and program counter .

In above there is symbol represent the Thread.
Thread provide synchronization : each thread shared data of other thread in same process.One thread can read, write or change another thread's data.

Types of Thread

There are two types of Thread
  1. User Level Threads -- User managed threads
  2. Kernel Level Threads -- Operating System managed threads acting on kernel, an operating system core.
But in this topic I haven't discuss because it's require 100 page to explain this operating system concept.




Life Cycle of a Thread (Thread States)

A thread have five state and it's have only one state at a time
A thread can be in one of the five states in the thread. According to sun, there is only 4 states new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The thread states are as follows:
  1. New
  2. Runnable
  3. Running
  4. Waiting (Blocked)
  5. Dead

1) New

When you create the instance of Thread Object.

2) Runnable

When you invoke the start() method thread object ,but scheduling algorithm have not been selected this thread.

3) Running

It's start it's working and get scheduled by scheduling algorithm of operating system 

4) Waiting

This is the state when the thread is still alive, but is currently not eligible to run.

5) Dead

A thread is in dead state when object of thread become out of scope and garbage collector sweep it from system outsources.


Implementation Thread in JAVA

You can implement Thread in Java be two ways
  1. extends Thread class
  2. implement runnable interface
Here are code example

public class ThreadA implements Runnable {
    public void run() {
     //Code
    }
}
 public class ThreadA extends Thread {
    public void run() {
     //Code
    }
}





you can start thread by just invoking start() method to make it into running state.
  
ThreadA myThread=new ThreadA();
myThread.start();