Monday 26 July 2010

Using JDBC in the Project (Project Architecture)



Fig: JdbcExample1(ProjectArchitec).JPG

Low Level means it is specific to one type of dependent persistence logic
Persistence Logic: is responsible for accessing persistence Data
·         If I’m using low level persistence logic in Business Logic (i.e High Level Logic) so I can not connect to all the Back end servers.
To avoid this problem we have to use another layer , keep the persistence logic in a separate object,  that object is nothing but a Data Access Object (DAO).

SO here DAO is a Design Pattern, Design Pattern which gives a solution for a problem.
(Example: I’m facing a problem, I will try to interact with a friend who is already faced the problem because he will have ready made solution for that problem, no need to waste my time to solve the problem, because already ready made solution (design pattern) is available 


Fig: DAO DesignPattern.JPG

·         When it comes into the project (enterprise application) we need to concentrate in optimizing the code, making the more reusable, and also testable.
·         The DAO is the most common pattern implemented into the enterprise applications.
What is a Pattern?
·         Pattern is a three part document that describes the context (situation), reoccurring problem and the best solution for the problem in the given context (situation).
·         The software patterns are categorized into multiple type for convenience in learning and applying.
·         The design patterns is one of the category. This lists the patterns describing the problems related to design and development of the software applications
THE DAO DESIGN PATTERN:

·         As the title describes this is a design pattern
Context (situation):
·         We want to create enterprise Application with reasonable business logic having a requirement of accessing multiple (variety) of datastore and /or found that may have a requirement of migrating from one type of data store (database) to other. Easy to migrate between the different types of data stores. That is in my application I want to connect to LDAP to take credentials, same application I want to connect to File Systems to accept login details.
Problem:
·         We want to separate the low-level persistence (data access) logic from Business Logic. But this so this solution leaves a problem that is how do you separate in a best way? Thus this solution we kept in a problem.
Forces: (Why):
·          We want to have a proper responsibility division that is:
             (a) improves the quality of the system.
             (b) reduces the cost and time for the development
·         To enable unit testing, make the system more comfortable for testing.
·         Easy to migrate between the different types of data stores.
Solution:
·         Implement the low-level persistence logic into a separate object, exposing the data access operations through high-level API to the service layer.
What is DAO ?
Ans: DAO is a design pattern that describes separating the low-level persistence logic from the business logic, implementing into a separate (independent) object.

Note: this special object introduced implementing the DAO pattern is also refered as a DAO i.e: Data Access Object

Fig: DAO DesignPattern.JPG

From this discussion and the above architecture we understand JDBC is used for implementing the DAO in Java for accessing the tabular data store

Implementing DAO Design Pattern in our project
Use Case Diagram of EMPLOYEE MANAGEMENT SYSTEM
Fig: DAO DesignPattern1(a).JPG


Fig: DAO DesignPattern1.JPG

For Example:
Implementing the Data Access Layer for ‘CreateEmployee’ use case of ‘Employee Management System (EMS).

//EmployeeDAOI.java
package com.st.ems.dao;
public interface EmployeeDAOI
{
 void save(int eno,String name, double sal, int dno);
//we will change this struture later
 //we will add some more methods as we proceed
}


//EmployeeDAO.java
package com.st.ems.dao.jdbc;
import com.st.ems.dao.EmployeeDAOI;
import java.sql.*;
import java.util.*;
public class EmployeeDAO implements EmployeeDAOI
{
 public void save(int eno, String name,double sal, int dno)
 {
  //this method is responsible for saving the given details into emp table
  //to do: execute the following SQL
  String sql="insert into emp values("+eno+",'"+name+",',"+sal+","+dno+")";
  //how to execute?
  //use JDBC
  //Write this Connection con=null here only to make visible to all the blocks
  Connection con=null;//null is necessary whn u r declaring as a local variable
  try
  {
   //step 1.1
   String driverClassName="oracle.jdbc.driver.OracleDriver";//here we are using oracle driver
   Class c=Class.forName(driverClassName);
   Driver d=(Driver)c.newInstance();
   //step 1.2
   String jdbcUrl="jdbc:oracle:thin:@localhost:1521:XE";
   Properties p=new Properties();
   p.put("user","system");
   p.put("password","manager");
   //Connection con=null;//i can not use con ref variable in finally block as it is local to this
//block

  con=d.connect(jdbcUrl,p);
  //step2
  Statement st=con.createStatement();
  //step3
  st.executeUpdate(sql);


  }//end of try block
  catch(Exception e)
  {
   e.printStackTrace();
   //to report the error, we will set run time error
   throw new RuntimeException(e);
  }
  finally
  {
   try
   {
    //step 4:
    con.close();

   }//try
  catch(Exception e){}
  }//finally


 }//save
}//class

/* now we are writing Tese case for DAO object [that is save()] for this we have to use JUNIT but
we are using main() for this application
*/ 
//EmployeeDAOTestCase.java
import com.st.ems.dao.jdbc.EmployeeDAO;
import com.st.ems.dao.EmployeeDAOI;


public class EmployeeDAOTestCase
{
 private EmployeeDAOI edao;
 public void testsave()
 {
  edao.save(102,"e102",20000,20);
  System.out.println("Details saved");
 }
 public static void main(String s[])
 {
  EmployeeDAOTestCase test=new EmployeeDAOTestCase();
  test.edao=new EmployeeDAO();
  test.testsave();//here Driver object is created
//test.testsave();//here 2nd Driver object is created but one Driver object is enought to handle
//multiple request from diffrent clients, connections as it is a Thread -safe
 }
}

/*
D:\material\java(E DRIVE)\java\AdvJava\jdbc\DAO>javac -d . *.java
D:\material\java(E DRIVE)\java\AdvJava\jdbc\DAO>D:\material\java(E
DRIVE)\java\AdvJava\jdbc\DAO>set classpath=C:\oraclexe\app\or
acle\product\10.2.0\server\jdbc\lib\ojdbc14.jar;.;
D:\material\java(E DRIVE)\java\AdvJava\jdbc\DAO>java EmployeeDAOTestCase
Details saved
*/

Fig: DesignPattern2.JPG

Q: is our DAO created efficient?
Ans: No, we need to multiple changes. Let us look into all of them one after the other
·         In the EmployeeDAO created earlier the save() method is programmed to create a new instance (object) of Driver class on every request, which is not effective.
·         Considering the following points with respect to the Driver:
(1)   The Driver object is Thread-safe means Driver object performs consistently even on concurrent requests from multiple threads
(2)   A single instance of Driver can be used to create multiple connections because it is Thread-safe. If we create the multiple instances of Driver class, unnecessarly garbage is stored into the memory, and performance becomes slow.
(3)   Considering these points a single instance of Driver is enough for an application per data base.

Fig: FactoryClass(EmpDao.JPG

To overcome this problem we have to go for Driver Manager





No comments:

Post a Comment