ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jean-Francois Poilpret" <jfpoilp...@hcm.vnn.vn>
Subject RE: Atomic transaction in iBATIS
Date Mon, 06 Mar 2006 15:40:10 GMT
Hi Tony,

 

I don't think transaction boundaries are the concerns of DAO.

Generally stated transaction is a "cross-cutting concern".

If you use EJB or one lightweight container like Spring or HiveMind then
transactions are supported _declaratively_ at any level (but preferrably you
should put them at service level).

All the containers above are able to create a transaction if no one exists
at call time, or reuse the current transaction if one is already open. And
that happens without any transaction management code anywhere!

 

In any case (I mean: even if you don't use containers, be it EJB or
lightweight), it is never a good idea to hard-code the transaction
boundaries at the DAO level, because your DAO has only one responsibility:
access one piece of data of your DB independently of any transaction.
Putting transactions there would be giving too much responsibility to it
(and prevent higher level services to define wider transactions).

Transactions will most often embed many calls to several DAOs, and you have
to put your transaction boundaris to where they belong (ie the services that
call the DAO).

 

Cheers

 

            Jean-Francois

 

  _____  

From: Tony Qian [mailto:daqiqian2@aol.com] 
Sent: Monday, March 06, 2006 10:03 PM
To: user-java@ibatis.apache.org
Subject: Atomic transaction in iBATIS

 

All,

I need an atomic transaction for inserting data to several tables. iBATIS
supports that. I looked at the jpetstore code. I found that atomic
transaction could be made at both service and DAO level. Here are some
jpetstore codes.


in OrderService.java
  public void insertOrder(Order order) {
    try {
      // Get the next id within a separate transaction
      order.setOrderId(getNextId("ordernum"));

      daoManager.startTransaction();

      itemDao.updateAllQuantitiesFromOrder(order);
      orderDao.insertOrder(order);

      daoManager.commitTransaction();
    } finally {
      daoManager.endTransaction();
    }
  }

in AccountSqlMapDao.java
  public void insertAccount(Account account) {
    update("insertAccount", account);     //  btw, why is update? should it
be insert? it seems to me that actual action is controlled by action id not
by key word (update)
    update("insertProfile", account);
    update("insertSignon", account);
  }

in AccountService.java
 public void insertAccount(Account account) {
    accountDao.insertAccount(account);
  }


insertOrder is apparently an atomic transaction, which is enforced at
service level. My question is whether insertAccount is an atomic
transaction, i.e. what if "update("insertSignon", account);" fails? Should
we enforce it at DAO level (put three updates between startTransaction and
endTransaction)? or we can do same thing as inserOrder, i.e. put
"accountDao.insertAccount(account);" between daoManager.startTransaction and
daoManager.endTransaction.

If we use Abator to generate the DAO code, it makes sense that atomic
transaction is enforced at service level. But from software architecture
point of view, atomic transaction should be pushed down to DAO level. any
suggestions?

thanks,
Tony


Mime
View raw message