QSO Technologies Paying attention to details
DAO PATTERN By: Parveen Mudgil
QSO Technologies Paying attention to details
Architecture Overview • Types of Architecture: • 2 – tier Architecture • 3 – Tier Architecture • N – Tier Architecture
• What is difference between MVC pattern and 3 – Tier Architecture . • J2ee is specifications for building a enterprises system.
QSO Technologies Paying attention to details
Architecture as per j2ee Specification
QSO Technologies Paying attention to details
Application with All The Components of Business Application
QSO Technologies Paying attention to details
Container Architecture
QSO Technologies Paying attention to details
Container Architecture with API’s
QSO Technologies Paying attention to details
Problem You want to encapsulate data access and manipulation in a separate layer.
QSO Technologies Paying attention to details
Brief Description of Problem • It may be like one of the business application may need a RDBMS data store and others may need the data that reside in mainframe or LDAP etc . • There is even greater variation with different types of persistent storage. Access mechanisms, supported APIs, and features vary between different types of persistent stores such as RDBMS, object-oriented databases, flat files, and so forth.
QSO Technologies Paying attention to details
Forces • The problem of proprietary which may lead to tight coupling between the components and data sources. (any change in data source may lead to change in components)
Forces: • You want to implement data access mechanisms to access and manipulate data in a persistent storage. • You want to decouple the persistent storage implementation from the rest of your application.
QSO Technologies Paying attention to details
Forces • You want to provide a uniform data access API for a persistent mechanism to various types of data sources, such as RDBMS, LDAP, OODB, XML repositories, flat files, and so on. • You want to organize data access logic and encapsulate proprietary features to facilitate maintainability and portability.
QSO Technologies Paying attention to details
Solution
DAO Pattern : U se Data Access Object (DAO) to abstract and encapsulate all access to the data source. The DAO manages the connection with the data source to obtain and store data.
QSO Technologies Paying attention to details
Brief Explanation • In computer software, a(DAO) is an object that provides an abstract interface to some type of database or persistence mechanism, providing some specific operations without exposing details of the database. This isolation separates the concerns of what data accesses the application needs, in terms of domain-specific objects and data types (the public interface of the DAO), and how these needs can be satisfied with a specific DBMS, database schema, etc. (the implementation of the DAO).
QSO Technologies Paying attention to details
Brief Explanation • The DAO implements the access mechanism required to work with the data source. The data source could be a persistent store like an RDBMS, an external service like a B2B exchange, a repository like an LDAP database, or a business service accessed via CORBA Internet Inter-ORB Protocol (IIOP) or low-level sockets. • The business component that relies on the DAO uses the simpler interface exposed by the DAO for its clients.
QSO Technologies Paying attention to details
Brief Explanation • The DAO completely hides the data source implementation details from its clients. • the interface exposed by the DAO to clients does not change when the underlying data source implementation changes, this pattern allows the DAO to adapt to different storage schemes without affecting its clients or business components. (CR U D)
QSO Technologies Paying attention to details
Class Diagram
QSO Technologies Paying attention to details
Sequence Diagram
QSO Technologies Paying attention to details
Definitions • BusinessObject:The Business Object represents the data client. It is the object that requires access to the data source to obtain and store data. A Business Object may be implemented as a session bean, entity bean, or some other Java object, in addition to a servlet or helper bean that accesses the data source. • DataAccessObject :The DataAccessObject is the primary object of this pattern. The DataAccessObject abstracts the underlying data access implementation for the BusinessObject to enable transparent access to the data source. The BusinessObject also delegates data load and store operations to the DataAccessObject.
QSO Technologies Paying attention to details
Definitions • DataSource :This represents a data source implementation. A data source could be a database such as an RDBMS, OODBMS, XML repository, flat file system, and so forth. A data source can also be another system (legacy/mainframe), service (B2B service or credit card bureau), or some kind of repository (LDAP). • TransferObject: This represents a Transfer Object used as a data carrier. The DataAccessObject may use a Transfer Object to return data to the client. The DataAccessObject may also receive the data from the client in a Transfer Object to update the data in the data
QSO Technologies Paying attention to details
Strategy • Each BusinessObject corresponds to a specific DAO, it is possible to establish relationships between the BusinessObject, DAO, and underlying implementations (such as the tables in an RDBMS). • There are few tools available which are capable of generating DAO code. • The tools basically uses the factory method or Abstract factory pattern for the code generation based on the
QSO Technologies Paying attention to details
Factory Pattern • This strategy is used when we know that the data store will not change for our applications.
QSO Technologies Paying attention to details
Abstract Factory Pattern
QSO Technologies Paying attention to details
Abstract Factory Pattern • This strategy is followed when under lying data store can change for the applications . • This class diagram shows a base DAO factory, which is an abstract class that is inherited and implemented by different concrete DAO factories to support storage implementation specific access. • The client can obtain a concrete DAO factory implementation such as RdbDAOFactory and use it to obtain
QSO Technologies Paying attention to details
Sequence Diagram
QSO Technologies Paying attention to details
Advantages • Improved efficiency and performance of the data layer since it is standard reusable software. • It is also expected that in case the DAO implementation were to change the other parts of the application would be unaffected. • Resources are dedicated to develop and implement this layer which converts into better software in this layer. • Enables Transparency • Enables Easier Migration • Reduces Code Complexity in Business Objects • Centralizes All Data Access into a Separate Layer
QSO Technologies Paying attention to details
Disadvantages • As with many design patterns, a design pattern increases the complexity of the application. • As is common in Java, there are many opensource and commercial implementations of DAO available. Each of these can have potentially different implementation strategies and usage models. There is a familiarisation curve involved with each of them. • A certain amount of skill is required to
QSO Technologies Paying attention to details
Advance DAO • Till now we have discussed about • A DAO factory class • A DAO interface • A concrete class that implements the DAO interface • Data transfer objects (sometimes called value objects)
• Now let go more into low level implementation: Transaction Demarcation – The important thing to remember about
QSO Technologies Paying attention to details
Strategy • There are two main strategy followed for it. – the DAO responsible for demarcating transactions • In this strategy you have to embed the transaction inside the DAO classes. • public void createWarehouseProfile(WHProfile profile); public void updateWarehouseStatus(WHIdentifier id, StatusInfo status);
– transaction demarcation to the object that is calling the DAO's methods. • transaction demarcation code will be external to the DAO class
QSO Technologies Paying attention to details
Strategy Contd. • In the latter strategy the user combines multiple DAO object in one transaction. This transaction demarcation strategy is especially valuable for applications that need to access multiple DAOs in a single transaction. • This can be implemented : – JDBC API (simpler) – JTA (java transaction API – more flexible)
QSO Technologies Paying attention to details
Transaction demarcation with JDBC • JDBC transactions are controlled using the Connection object. • The JDBC Connection interface (java.sql.Connection) provides two transaction modes: auto-commit and manual commit. » » » »
public public public public
void setAutoCommit(boolean) boolean getAutoCommit() void commit() void rollback()
QSO Technologies Paying attention to details
import java.sql.*; import javax.sql.*; DataSource ds = obtainDataSource(); Connection conn = ds.getConnection(); conn.setAutoCommit(false); pstmt = conn.prepareStatement("UPDATE MOVIES ..."); pstmt.setString(1, "The Great Escape"); pstmt.executeUpdate(); conn.commit();
•
you can write multiple sql statement in single transaction • Limitation: you cannot span multiple databases. Connection is limited to single database.
QSO Technologies Paying attention to details
Transaction demarcation with JTA • JTA and JTS provide distributed transaction services for the J2EE platform. • Resources manger is a kind of data store
QSO Technologies Paying attention to details
Procedure • the application invokes methods on the javax.transaction.UserTransaction interface. • JNDI lookup for the UserTransaction object. import javax.transaction.*; import javax.naming.*; InitialContext ctx = new InitialContext(); Object txObj = ctx.lookup("java:comp/UserTransaction"); UserTransaction utx = (UserTransaction) txObj;
• After the application has a reference to the UserTransaction object it may start the transaction.
QSO Technologies Paying attention to details
Contd. utx.begin(); DataSource ds = obtainXADataSource(); Connection conn = ds.getConnection(); pstmt = conn.prepareStatement("UPDATE MOVIES ..."); pstmt.setString(1, "Spinal Tap"); pstmt.executeUpdate(); utx.commit();
• The javax.transaction.UserTransaction interface provides the following transaction control methods: » » » » » »
public void begin() public void commit() public void rollback() public int getStatus() public void setRollbackOnly() public void setTransactionTimeout(int)
QSO Technologies Paying attention to details
Procedure Contd. • To start a transaction the application calls begin(). To end a transaction the application calls either commit() or rollback(). • Developers often use JDBC for low-level data operations in DAO classes. • For transactions with JTA, you will need a JDBC driver that implements the javax.sql.XADataSource, javax.sql.XAConnection, and javax.sql.XAResource interfaces. • A driver that implements these interfaces will be able to participate in JTA transactions.
QSO Technologies Paying attention to details
Procedure Contd. • An XADataSource object is a factory for XAConnection objects. XAConnections are JDBC connections that participate in JTA transactions. • XA connections are different from nonXA connections
QSO Technologies Paying attention to details
RECAP • Embed DAO Approach: – Transaction demarcation code is embedded inside the DAO class. – The DAO class uses the JDBC API for transaction demarcation. – The caller has no way to demarcate the transaction. – Transaction scope is limited to a single JDBC Connection.
• JTA Approach: – Transactions are demarcated with JTA. – Transaction demarcation code is separated from the DAO. – The caller is responsible for demarcating the transaction.
QSO Technologies Paying attention to details
A Small Walk Through
(My first month at QSO Technologies) • Got the Rapid Solution as Project assignment. • As per first direction from I started reading about PAI and IAP . I studied documents related to its architecture , functional model , components subcomponents and how they followed j2ee specification in there product . ( but many things were not clearly mentioned as proprietary product). • I compared standard j2ee specification
QSO Technologies Paying attention to details
Contd. • Than I started going through the concepts of j2ee to understand it . And technologies related to it so that I have various components of PAI. • After discussing my views with sir and problem areas . Sir discussed one of the problem which I should look up for solution . • The problem was of migration of one database to other and handling
QSO Technologies Paying attention to details
Contd. • From reading material on data warehousing and data mining and discussion with my friends and professors in college I came across two technologies • DAO Patttern • ETL
• Than read material on standardize and non standardize applications.
QSO Technologies Paying attention to details
Reference • • • • • • • • •
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAcce http://java.sun.com/blueprints/patterns/DAO.html http://www.corej2eepatterns.com/Patterns2ndEd/DataAccessObjec http://www.codefutures.com/products/firestorm/benefits/ http://authors.phptr.com/corej2eepatterns/codeChap9.html#ex9.1 http://www.junlu.com/msg/31393.html http://www.ibm.com/developerworks/library/j-dao/ http://en.wikipedia.org/wiki/Data_Access_Object http://daoexamples.sourceforge.net/index.html
QSO Technologies Paying attention to details
Books References • Professional Java server programming volume 1 ( wrox publication) • The j2ee 5 tutorial for sun java application server 9.1 • Head first design patterns • Gang of four
QSO Technologies Paying attention to details
THANKS