ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeff Butler" <jeffgbut...@gmail.com>
Subject Re: Atomic transaction in iBATIS
Date Tue, 07 Mar 2006 17:43:14 GMT
Yes, your examples will make the updates happen in a single transaction.
iBATIS guarantees a single transaction as long as you are not using an
EXTERNAL transaction manager.

If you use an EXTERNAL transaction manager, then iBATIS does not do commits
or rollbacks - even if you call the methods, they do nothing at all -
because you've declared you are using an external transaction manager.

A good example would be container managed transactions in a session EJB.  In
that case, the EJB container manages the transactions and iBATIS does
nothing at all related to transactions.  You must code your EJB deployment
descriptor properly to define the transactional behavior of your system.

Jeff Butler



On 3/7/06, Tony Qian <daqiqian2@aol.com> wrote:
>
> Jeff,
>
> thanks for tips. My question is how to make account insertion atomic in
> jpetstore example. Will following code makes atomic transaction?
>
> in AccountService.java
>  public void insertAccount(Account account) {
>    try{
>     daoManager.startTransaction();
>     accountDao.insertAccount(account);
>
>     daoManager.commitTransaction();
>     } finally {
>       daoManager.endTransaction();
>    }
>
>      public void insertAccount(Account account) {
>     update("insertAccount", account);
>
>     update("insertProfile", account);
>     update("insertSignon", account);
>   }
>
>
>
> Or we have to bring up inserting Profile and inserting Sigon to service
> layer . For example,
>
> public void insertAccount(Account account) {
>    try{
>     daoManager.startTransaction();
>     accountDao.insertAccount(account);             // DAO only inserts
> data to account table
>     profileDao.insertProfile(account);                   //  DAO only
> inserts data to profile table
>     sigonDao.insertSignon(account);                  // DAO only inserts
> data to signon table
>
>     daoManager.commitTransaction();
>     } finally {
>       daoManager.endTransaction();
>    }
>
>
> If we don't use daoManager and external transaction manager, how to make
> the transaction atomic using iBATIS in above situation?
>
> thanks,
> Tony
>
> Jeff Butler wrote on 3/6/2006, 2:38 PM:
>
>  In iBATIS, each call is a seperate transaction unless you override it.
> You can override with your own transaction management (DAO or SqlMap API
> calls), or use an external transaction manager.
>
> The method you mention above (AccountService.insertAccount()) will result
> in three seperate transactions unless there is an external transaction
> manager.  This is why it is a pretty good practice to always wrap the DAO
> calls in the service layer in iBATIS transaction management code.  If there
> was an external transaction manager, that code would be ignored.  If there
> was not an external transaction manager, then using the iBATIS transaction
> API would guarantee a single transaction for each service method (a good
> idea IMHO).
>
> So I think the jpetstore example is not really correct in this instance.
>
> BTW - I would always control transactions in the service layer, never in
> the DAO layer.  This will work well with Abator too.
>
> Jeff Butler
>
>
>
> On 3/6/06, Tony Qian <daqiqian2@aol.com> wrote:
> >
> >
> >
> > Jean-Francois Poilpret wrote on 3/6/2006, 10:40 AM:
> >
> > in AccountService.java
> >  public void insertAccount(Account account) {
> >     accountDao.insertAccount(account);
> >   }
> >
> > Thank you very much for your response.  Do you know if  above code
> > guarantee an atomic transaction in iBATIS?  accountDao.insertAccount(account)
> > calls three updates (more accurately three inserts to different tables).
> >
> > thanks,
> > Tony
> >
>
>

Mime
View raw message