Beruflich Dokumente
Kultur Dokumente
DAO PATTERN
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
Container Architecture
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
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
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
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
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
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 non-
XA 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
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