Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » J2EE Pattern »

Re: DAO pattern

Tareq

2004-11-22

Replies:

Your PM may need to be educated on software patterns and architecture.

The DAO pattern encapsulates data access to a data source. First, your
design should be interface driven which would mandate using concrete
classes. Second, having a DAO implementation can assist in varying
implementations. Third, Testability - I am assuming you do unit testing
and to facilitate unit test components dependent on the DAO layer you
would need mock objects (eg. Look at Spring's use of dependency
injection)

T.

-----Original Message-----
From: An interest list for Sun Java Center J2EE Pattern Catalog
[mailto:J2EEPATTERNS-INTEREST@(protected)
Sent: Monday, November 22, 2004 10:35 PM
To: J2EEPATTERNS-INTEREST@(protected)
Subject: Re: DAO pattern

Hi Ananthalakshmi,

 Thanks for the code explanation and the same to group members. One
more
small question. My PM suggesting me to use static methods in DAO class.
what is the difference (In the design perspective) by putting the DAO's
methods as static and non-static? Is there any impact if we keep the
methods as static in a web application?

Regards
Darshan

On Sun, 21 Nov 2004 06:54:53 -0700, Ananthalakshmi Subramaniyam
<ananthalakshmi_mca@(protected):

>On Thu, 18 Nov 2004 23:11:06 -0700, Darshan Kashi
<dkumar@(protected)>
>wrote:
>
>>Hi,
>>
>> Thanks for your reply. I just gone through the link you provided, and
its
>>really useful for me. There is one point i need some clarification.
>>
>>In that article the author mentioned two approaches
>>
>>1. Transaction demarcation using JDBC
>>     (The <B>caller has no way to demarcate</B> the transaction)
>>2. Transaction demarcation using JTA
>>     (The <B>caller is responsible for demarcating</B> the
transaction)
>>
>>I need to control the trasaction from the caller(not in DAO), and at
the
>>same time i don't want to go for JTA. Is it not possible?
>>
>>Regards
>>Darshan
>>
>>
>>On Thu, 18 Nov 2004 09:24:35 -0300, German M. Gomez
>><german_m_gomez@(protected):
>>
>>>I believe you will find this link useful http://www-
>>106.ibm.com/developerworks/java/library/j-dao/
>>>
>>>See Transaction demarcation
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>___________________________________
>>>?Llevate a Yahoo! en tu Unifó®¡ >>Ahora podé³ usar Yahoo! Messenger
en tu
>Unifó®¬ en cualquier momento y
>>lugar.
>>>Encontrá ­á³ informació® ¥n: http://ar.mobile.yahoo.com/sms.html
>>>
>>>====================================================================
>>>Companion Site: http://www.corej2eepatterns.com
>>>J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
>>>List Archive: http://archives.java.sun.com/archives/j2eepatterns-
>>interest.html
>>>Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to
>>listserv@(protected)
>>
>>====================================================================
>>Companion Site: http://www.corej2eepatterns.com
>>J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
>>List Archive: http://archives.java.sun.com/archives/j2eepatterns-
>interest.html
>>Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to
>listserv@(protected)
>
>Hi,
>
>Normally, it is best practice to handle Transaction demarcation outside
of
>DAO, in order to demarcate Transaction between multiple DAOs. This
approach
>is more flexible.
>
>For example,
>
>public class UserTransaction {
>  Connection conn = null;
>  String strUser = null;
>  String strDbUrl = null;
>  String strDbUser = null;
>  String strPassword = null;
>
>  public UserTransaction(String strURL, String strUser,
>                 String strPassword) {
>   this.strDbUrl = strURL;
>   this.strDbUser = strUser;
>   this.strPassword = strPassword;
>  }
>  public void beginTransaction(...) {}
>  public void endTransaction(...) {}
>  public Connection getConnection() {return connection;}
>  public void commit() {}
>  public void rollback() {}
>}
>public class MyDAOFactory {
>  public static UserTransaction transaction = null;
>  public static final String DRIVER=
>   "COM.cloudscape.core.RmiJdbcDriver";
>  public static final String DBURL=
>   "jdbc:cloudscape:rmi://localhost:1099/CoreJ2EEDB";
>  public static final String USER ="user";
>  public static final String PASSWORD = "pwd";
>
>  public static UserTransaction getUserTransaction() {
>   if(transaction == null ) {
>     UserTransaction transaction = new UserTransaction
>(DRIVER,DBRUL,USER,PASSWORD);
>   }
>   return transaction;
>  }
>  ...
>  public AccountDAO getAccountDAO();
>}
>public class AccountDAO {
>  public void credit() {
>   UserTransaction transaction =
>     MyDAOFactory.getUserTransaction();/use existing user transaction
or
>                            create new one.
>   Connection connection = transaction.getConnection();
>   Statement stmt = connection.createStatement(sqlQuery);
>   //do the operations.
>  }
>  public void debit() {}
>  ...
>}
>
>In Caller class,
>
>MyDAOFactory daoFactory = new MyDAOFactory();
>UserTransaction transaction = MyDAOFactory.getUserTransaction();
>transaction.beginTransaction();
>AccountDAO accountDAO = daoFactory.getAccountDAO();
>try {
>accountDAO.credit();
>accountDAO.debit();
>...
>transaction.commit();
>} catch(myDAOException ex) {
> transaction.rollback();
>}
>transaction.endTransaction();
>
>The above approach is helpful in both DAO demarcated Transaction and
>caller demarcated transaction. ie, in DAO demarcated methods, you may
have
>the following implementation,
>
>public void autonomousCredit() {
>   UserTransaction transaction =
>     MyDAOFactory.getUserTransaction();/use existing user transaction
or
>                            create new one.
>   transaction.beginTransaction();
>   Connection connection = transaction.getConnection();
>   Statement stmt = connection.createStatement(sqlQuery);
>   try {
>    //do the operations.
>    transaction.commit();
>   } catch(Exception ex) {
>    transaction.rollback();
>   }
>   transaction.endTransaction();
>}
>
>Thanks,
>H.Ananthalakshmi.Hi,
>
>Normally, it is best practice to handle Transaction demarcation outside
of
>DAO, in order to demarcate Transaction between multiple DAOs. This
approach
>is more flexible.
>
>For example,
>
>public class UserTransaction {
>  Connection conn = null;
>  String strUser = null;
>  String strDbUrl = null;
>  String strDbUser = null;
>  String strPassword = null;
>
>  public UserTransaction(String strURL, String strUser,
>                 String strPassword) {
>   this.strDbUrl = strURL;
>   this.strDbUser = strUser;
>   this.strPassword = strPassword;
>  }
>  public void beginTransaction(...) {}
>  public void endTransaction(...) {}
>  public Connection getConnection() {return connection;}
>  public void commit() {}
>  public void rollback() {}
>}
>public class MyDAOFactory {
>  public static UserTransaction transaction = null;
>  public static final String DRIVER=
>   "COM.cloudscape.core.RmiJdbcDriver";
>  public static final String DBURL=
>   "jdbc:cloudscape:rmi://localhost:1099/CoreJ2EEDB";
>  public static final String USER ="user";
>  public static final String PASSWORD = "pwd";
>
>  public static UserTransaction getUserTransaction() {
>   if(transaction == null ) {
>     UserTransaction transaction = new UserTransaction
>(DRIVER,DBRUL,USER,PASSWORD);
>   }
>   return transaction;
>  }
>  ...
>  public AccountDAO getAccountDAO();
>}
>public class AccountDAO {
>  public void credit() {
>   UserTransaction transaction =
>     MyDAOFactory.getUserTransaction();/use existing user transaction
or
>                            create new one.
>   Connection connection = transaction.getConnection();
>   Statement stmt = connection.createStatement(sqlQuery);
>   //do the operations.
>  }
>  public void debit() {}
>  ...
>}
>
>In Caller class,
>
>MyDAOFactory daoFactory = new MyDAOFactory();
>UserTransaction transaction = MyDAOFactory.getUserTransaction();
>transaction.beginTransaction();
>AccountDAO accountDAO = daoFactory.getAccountDAO();
>try {
>accountDAO.credit();
>accountDAO.debit();
>...
>transaction.commit();
>} catch(myDAOException ex) {
> transaction.rollback();
>}
>transaction.endTransaction();
>
>The above approach is helpful in both DAO demarcated Transaction and
>caller demarcated transaction. ie, in DAO demarcated methods, you may
have
>the following implementation,
>
>public void autonomousCredit() {
>   UserTransaction transaction =
>     MyDAOFactory.getUserTransaction();/use existing user transaction
or
>                            create new one.
>   transaction.beginTransaction();
>   Connection connection = transaction.getConnection();
>   Statement stmt = connection.createStatement(sqlQuery);
>   try {
>    //do the operations.
>    transaction.commit();
>   } catch(Exception ex) {
>    transaction.rollback();
>   }
>   transaction.endTransaction();
>}
>
>Thanks,
>H.Ananthalakshmi.
>
>====================================================================
>Companion Site: http://www.corej2eepatterns.com
>J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
>List Archive: http://archives.java.sun.com/archives/j2eepatterns-
interest.html
>Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to
listserv@(protected)

====================================================================
Companion Site: http://www.corej2eepatterns.com
J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
List Archive:
http://archives.java.sun.com/archives/j2eepatterns-interest.html
Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to
listserv@(protected)

====================================================================
Companion Site: http://www.corej2eepatterns.com
J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns
List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html
Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.