ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher.Mathru...@sybase.com
Subject RE: Optimistic locking question
Date Thu, 16 Mar 2006 01:47:45 GMT

<br><font size=2 face="sans-serif">I would agree that this would be an
easy implementation that I would prefer. The problem is that I am working
with an existing Data Model that I inherited and I cannot change without
having an enormous impact on multiple groups and application UI's. (Not
something that I am ready to propose) &nbsp;This is the model that I am
stuck with unfortunately.</font>
<br>
<br><font size=2 face="sans-serif">I think I have a solution to the problem
but I feel that it is a very dirty one. I could define an additional field
in each domain object and call it 'now', which would get the current timestamp,
set by the Dao, just prior to performing the insert. I would then update
the value in the SQL as follows:</font>
<br>
<br><font size=2 color=#008080>&nbsp; &nbsp;update customer set <br>
 &nbsp; &nbsp; name = #name#, <br>
 &nbsp; &nbsp; record_date = #now#,</font><font size=3> </font><font
size=2 color=#008080><br>
 &nbsp; &nbsp; ...</font><font size=3> </font><font size=2 color=#008080><br>
 &nbsp; where</font><font size=3> </font><font size=2 color=#008080><br>
 &nbsp; &nbsp; id = #id#</font><font size=3> </font><font size=2
color=#008080><br>
 &nbsp; and</font><font size=3> </font><font size=2 color=#008080><br>
 &nbsp; &nbsp; record_date = #recordDate# &nbsp; </font><font size=2>&nbsp;</font><font
size=3>
<br>
</font>
<br><font size=3>Then if the update was successful I could have the Dao
update the record date with the value of now. It's dirty, so I am in hopes
that someone has a better solution for me.</font>
<br>
<br><font size=3>Thanks...</font>
<br><font size=2 face="sans-serif"><br>
Chris Mathrusse<br>
christopher.mathrusse@sybase.com<br>
Sybase, Inc<br>
One Sybase Drive<br>
Dublin, CA 94568<br>
(925) 236-5553</font>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td width=40%><font size=1 face="sans-serif"><b>&quot;MCCORMICK, Paul&quot;
&lt;Paul.McCORMICK@doir.wa.gov.au&gt;</b> </font>
<p><font size=1 face="sans-serif">03/15/2006 05:30 PM</font>
<table border>
<tr valign=top>
<td bgcolor=white>
<div align=center><font size=1 face="sans-serif">Please respond to<br>
user-java@ibatis.apache.org</font></div></table>
<br>
<td width=59%>
<table width=100%>
<tr>
<td>
<div align=right><font size=1 face="sans-serif">To</font></div>
<td valign=top><font size=1 face="sans-serif">&lt;user-java@ibatis.apache.org&gt;</font>
<tr>
<td>
<div align=right><font size=1 face="sans-serif">cc</font></div>
<td valign=top>
<tr>
<td>
<div align=right><font size=1 face="sans-serif">Subject</font></div>
<td valign=top><font size=1 face="sans-serif">RE: Optimistic locking question</font></table>
<br>
<table>
<tr valign=top>
<td>
<td></table>
<br></table>
<br>
<br>
<br><font size=2 color=blue face="Arial">Instead of using a timestamp I
use an integer and increment the value on every update.</font>
<br><font size=3>&nbsp;</font>
<br><font size=2 color=blue face="Arial">update customer</font>
<br><font size=2 color=blue face="Arial">set NAME= #name#,</font>
<br><font size=3>&nbsp; &nbsp; </font><font size=2 color=blue
face="Arial"><b>VERSION_ID
= VERSION_ID + 1</b> &nbsp;</font>
<br><font size=2 color=blue face="Arial">where ID = #ID# and <b>VERSION_ID
&nbsp;= #VERSION_ID#</b></font>
<br><font size=3>&nbsp;</font>
<br><font size=2 color=blue face="Arial">Then in the Dao I would increment
versionId value on the CustomerDTO object if the update succeeded.</font>
<br><font size=2 color=blue face="Arial">&nbsp; &nbsp; &nbsp; &nbsp;
int
rowCount = getSqlMapClientTemplate().update( &quot;Customer-update&quot;,
record);</font><font size=3> </font><font size=2 color=blue face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;<br>
 &nbsp; &nbsp; &nbsp; &nbsp;if(rowCount == 0) {</font><font size=3>
</font><font size=2 color=blue face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new OptimisticLockingFailureException(&quot;Optimistic
Lock Failure! Unable to update record: &quot; + record); &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;</font><font size=3> </font><font
size=2 color=blue face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;}</font><font size=3> </font>
<br><font size=2 color=blue face="Arial">&nbsp; &nbsp; &nbsp; &nbsp;
<b>record.setVersionId(record.getVersionId()
+ 1 );</b></font>
<br><font size=3><br>
 </font>
<br>
<hr><font size=2 face="Tahoma"><b>From:</b> Christopher.Mathrusse@sybase.com
[mailto:Christopher.Mathrusse@sybase.com] <b><br>
Sent:</b> Wednesday, 15 March 2006 11:55 PM<b><br>
To:</b> user-java@ibatis.apache.org<b><br>
Subject:</b> Optimistic locking question</font><font size=3><br>
</font>
<br><font size=2 face="sans-serif"><br>
I have a table:</font><font size=3> <br>
</font><font size=2 face="sans-serif"><br>
CREATE TABLE CUSTOMER</font><font size=3> </font><font size=2 face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;ID NUMBER NOT NULL,</font><font size=3>
</font><font size=2 face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;NAME VARCHAR(32) NOT NULL,</font><font
size=3>
</font><font size=2 face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;RECORD_DATE TIMESTAMP NOT NULL,</font><font
size=3>
</font><font size=2 face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;...</font><font size=3> <br>
</font><font size=2 face="sans-serif"><br>
The Record_Date is used to determine the last time the record was updated,
so when I perform an update my SQL would look something like:</font><font size=3>
<br>
</font><font size=2 color=#008080><br>
 &nbsp; update customer set <br>
 &nbsp; &nbsp; name = #name#, <br>
 &nbsp; &nbsp; record_date = getDate(),</font><font size=3> </font><font
size=2 color=#008080><br>
 &nbsp; &nbsp; ...</font><font size=3> </font><font size=2 color=#008080><br>
 &nbsp; where</font><font size=3> </font><font size=2 color=#008080><br>
 &nbsp; &nbsp; id = #id#</font><font size=3> </font><font size=2
color=#008080><br>
 &nbsp; and</font><font size=3> </font><font size=2 color=#008080><br>
 &nbsp; &nbsp; record_date = #recordDate# &nbsp; </font><font size=2>&nbsp;</font><font
size=3>
<br>
</font><font size=2 face="sans-serif"><br>
This allows me to do the following in my Dao:</font><font size=3> </font><font
size=2 face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;</font><font size=2 color=blue face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;int rowCount = getSqlMapClientTemplate().update(
&quot;Customer-update&quot;, record);</font><font size=3> </font><font
size=2 color=blue face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;<br>
 &nbsp; &nbsp; &nbsp; &nbsp;if(rowCount == 0) {</font><font size=3>
</font><font size=2 color=blue face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new OptimisticLockingFailureException(&quot;Optimistic
Lock Failure! Unable to update record: &quot; + record); &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;</font><font size=3> </font><font
size=2 color=blue face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;}</font><font size=3> </font><font
size=2 color=blue face="sans-serif"><br>
 &nbsp; &nbsp; &nbsp; &nbsp;<br>
 &nbsp; &nbsp; &nbsp; &nbsp;return rowCount; &nbsp; &nbsp; &nbsp;
&nbsp;</font><font size=3>
<br>
<br>
</font><font size=2 face="sans-serif"><br>
The problem that I have is if my SQL is performing the update of the record_date
field when the insert is performed, how do I update my domain object with
the new value without performing a read from the table?</font><font size=3>
<br>
<br>
</font><font size=2 face="sans-serif"><br>
Thanks for the help...</font><font size=3> </font><font size=2 face="sans-serif"><br>
<br>
Chris Mathrusse<br>
christopher.mathrusse@sybase.com<br>
Sybase, Inc<br>
One Sybase Drive<br>
Dublin, CA 94568<br>
(925) 236-5553</font><font size=3> </font>
<table width=100%>
<tr>
<td width=100% bgcolor=white><font size=3>&quot;DISCLAIMER: This email,
including any attachments, is intended only for use by the addressee(s)
and may contain confidential and/or personal information and may also be
the subject of legal privilege. If you are not the intended recipient,
you must not disclose or use the information contained in it. In this case,
please let me know by return email, delete the message permanently from
your system and destroy any copies. <br>
<br>
Before you take any action based upon advice and/or information contained
in this email you should carefully consider the advice and information
and consider obtaining relevant independent advice.</font></table>
<br>
<br>

Mime
View raw message