ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tony Qian" <daqiqi...@aol.com>
Subject Re: Atomic transaction in iBATIS
Date Tue, 07 Mar 2006 15:31:16 GMT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title></title>
</head>
<body>
<font face="Arial,sans-serif" size="2"><font size="2">Jeff,<br>
<br>
thanks for tips. My question is how to make account insertion atomic in
jpetstore example. </font></font><font face="Arial,sans-serif" size="2"><font
 size="2">Will following code makes atomic transaction?</font></font><br>
<font face="Arial,sans-serif" size="2"><font size="2"><br>
</font></font><font face="Arial,sans-serif" size="2"><span class="q"><font
 face="Arial" size="2"><font face="Arial"><font size="2">in
AccountService.java
<br>
&nbsp;public void insertAccount(Account account) {<br>
&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp; daoManager.startTransaction();<br>
&nbsp;&nbsp;&nbsp; accountDao.insertAccount(account);<br>
&nbsp;&nbsp;&nbsp; daoManager.commitTransaction();<br>
&nbsp;&nbsp;&nbsp; } finally {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; daoManager.endTransaction();<br>
&nbsp;&nbsp; }<br>
<br>
</font></font></font></span></font><font face="Arial,sans-serif"
 size="2"><font size="2">&nbsp; &nbsp; public void insertAccount(Account
account)
{<br>
&nbsp;&nbsp;&nbsp; update("insertAccount", account);<br>
&nbsp;&nbsp;&nbsp; update("insertProfile", account);<br>
&nbsp;&nbsp;&nbsp; update("insertSignon", account);<br>
&nbsp; }</font></font><br>
<font face="Arial,sans-serif"><font size="2"><br>
<br>
Or we have to bring up inserting Profile and inserting Sigon to service
layer . For example,<br>
<br>
</font></font><font face="Arial,sans-serif" size="2"><span class="q"><font
 face="Arial" size="2"><font face="Arial"><font size="2">public void
insertAccount(Account account) {<br>
&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp; daoManager.startTransaction();<br>
&nbsp;&nbsp;&nbsp; accountDao.insertAccount(account);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// <font  color="#0000ff">DAO only inserts data to account table</font><br>
&nbsp;&nbsp;&nbsp; profileDao.insertProfile(account);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//&nbsp; <font  color="#0000ff">DAO only inserts data to profile table</font><br>
&nbsp;&nbsp;&nbsp; sigonDao.insertSignon(account);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// <font  color="#0000ff">DAO only inserts&nbsp; data to signon table</font><br>
&nbsp;&nbsp;&nbsp; daoManager.commitTransaction();<br>
&nbsp;&nbsp;&nbsp; } finally {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; daoManager.endTransaction();<br>
&nbsp;&nbsp; }</font></font></font></span></font><br>
<font face="Arial,sans-serif"><font size="2"><br>
If we don't use daoManager and external transaction manager, how to
make the transaction atomic using iBATIS in above situation?<br>
<br>
thanks,<br>
Tony<br>
<br>
<span type="cite">Jeff Butler wrote on 3/6/2006, 2:38 PM:</span>
</font></font>
<p><font face="Arial,sans-serif" size="2"></font></p>
<blockquote type="cite"  style="border-left: thin solid blue; padding-left: 10px; margin-left:
0pt;"><font  face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">In iBATIS, each call is a
seperate transaction unless you override it.&nbsp; You can override with
your own transaction management (DAO or SqlMap API calls), or use an
external transaction manager.</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">&nbsp;</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">The method you mention
above (AccountService.insertAccount()) will result in three seperate
transactions unless there is an external transaction manager.&nbsp; This is
why it is a pretty good practice to always wrap the DAO calls in the
service layer in iBATIS transaction management code.&nbsp; If there was an
external transaction manager, that code would be ignored.&nbsp; 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).
  </font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">&nbsp;</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">So I think the jpetstore
example is not really correct in this instance.</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">&nbsp;</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">BTW - I would always
control transactions in the service layer, never in the DAO layer.&nbsp;
This will work well with Abator too.</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">&nbsp;</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2">Jeff Butler</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2"><br>
  <br>
&nbsp;</font></div>
  <font face="Arial,sans-serif" size="2"></font>
  <div><font face="Arial,sans-serif" size="2"><span class="gmail_quote">On
3/6/06, <b class="gmail_sendername">Tony Qian</b> &lt;<a  href="mailto:daqiqian2@aol.com">daqiqian2@aol.com</a>&gt;
wrote:</span>
  </font>
  <blockquote class="gmail_quote"  style="border-left: 1px solid rgb(204, 204, 204); margin:
0px 0px 0px 0.8ex; padding-left: 1ex;"><font  face="Arial,sans-serif" size="2"></font>
    <div style="direction: ltr;"><font face="Arial,sans-serif" size="2"><font
 size="2"><br>
    <br>
    <span type="cite">Jean-Francois Poilpret wrote on 3/6/2006, 10:40
AM:</span> </font></font></div>
    <font face="Arial,sans-serif" size="2"></font>
    <div style="direction: ltr;"><font face="Arial,sans-serif" size="2"><span
 class="q">
    <blockquote  style="border-left: thin solid blue; padding-left: 10px; margin-left:
0pt;"  type="cite"><font face="Arial" size="2"><font face="Arial"><font
 size="2">in AccountService.java
      <br>
&nbsp;public void insertAccount(Account account) {<br>
&nbsp;&nbsp;&nbsp; accountDao.insertAccount(account);<br>
&nbsp; }</font></font></font></blockquote>
    </span></font></div>
    <font face="Arial,sans-serif" size="2"></font>
    <div style="direction: ltr;"><font size="2" face="Arial,sans-serif"><font
 face="Arial,sans-serif">Thank you very much for your response.&nbsp; Do you
know if&nbsp; above code guarantee an atomic transaction in iBATIS?&nbsp; </font></font><font
 face="Arial" size="2">
    <font face="Arial"><font size="2">accountDao.insertAccount(account)
calls three updates (more accurately three inserts to different tables).<br>
    <br>
thanks,<br>
Tony <br>
    </font></font></font></div>
  </blockquote>
  </div>
  <font face="Arial,sans-serif" size="2"><br>
  </font></blockquote>
</body>
</html>


Mime
View raw message