You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by "MCCORMICK, Paul" <Pa...@doir.wa.gov.au> on 2006/03/16 02:30:00 UTC

RE: Optimistic locking question

Instead of using a timestamp I use an integer and increment the value on
every update.

update customer
set NAME= #name#,
    VERSION_ID = VERSION_ID + 1 
where ID = #ID# and VERSION_ID  = #VERSION_ID#

Then in the Dao I would increment versionId value on the CustomerDTO
object if the update succeeded.
        int rowCount = getSqlMapClientTemplate().update(
"Customer-update", record);
       
        if(rowCount == 0) {
            throw new OptimisticLockingFailureException("Optimistic Lock
Failure! Unable to update record: " + record);            
        }
        record.setVersionId(record.getVersionId() + 1 );


________________________________

From: Christopher.Mathrusse@sybase.com
[mailto:Christopher.Mathrusse@sybase.com]
Sent: Wednesday, 15 March 2006 11:55 PM
To: user-java@ibatis.apache.org
Subject: Optimistic locking question



I have a table:

CREATE TABLE CUSTOMER
        ID NUMBER NOT NULL,
        NAME VARCHAR(32) NOT NULL,
        RECORD_DATE TIMESTAMP NOT NULL,
        ...

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:

   update customer set
     name = #name#,
     record_date = getDate(),
     ...
   where
     id = #id#
   and
     record_date = #recordDate#    

This allows me to do the following in my Dao:
       
        int rowCount = getSqlMapClientTemplate().update(
"Customer-update", record);
       
        if(rowCount == 0) {
            throw new OptimisticLockingFailureException("Optimistic Lock
Failure! Unable to update record: " + record);            
        }
       
        return rowCount;        


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?


Thanks for the help...

Chris Mathrusse
christopher.mathrusse@sybase.com
Sybase, Inc
One Sybase Drive
Dublin, CA 94568
(925) 236-5553


"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.

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.

Re: Optimistic locking question

Posted by netsql <ne...@roomity.com>.
Sybase has timestamp just for that.
.V

Christopher.Mathrusse@sybase.com wrote:
> 
> 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)  This is the model that I am stuck with unfortunately.
> 
> 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:
> 
>    update customer set
>     name = #name#,
>     record_date = #now#,
>     ...
>   where
>     id = #id#
>   and
>     record_date = #recordDate#    
> 
> 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.
> 
> Thanks...
> 
> Chris Mathrusse
> christopher.mathrusse@sybase.com
> Sybase, Inc
> One Sybase Drive
> Dublin, CA 94568
> (925) 236-5553
> 


Re: Optimistic locking question

Posted by Larry Meadors <lm...@apache.org>.
I think that is most likely your best bet.

Larry


On 3/15/06, Christopher.Mathrusse@sybase.com <
Christopher.Mathrusse@sybase.com> wrote:
>
>
> 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)
>  This is the model that I am stuck with unfortunately.
>
> 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:
>
>    update customer set
>     name = #name#,
>      record_date = #now#,
>
>     ...
>   where
>     id = #id#
>   and
>     record_date = #recordDate#
>
> 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.
>
> Thanks...
>
>
> Chris Mathrusse
> christopher.mathrusse@sybase.com
> Sybase, Inc
> One Sybase Drive
> Dublin, CA 94568
> (925) 236-5553
>
>
>  *"MCCORMICK, Paul" <Pa...@doir.wa.gov.au>*
>
> 03/15/2006 05:30 PM  Please respond to
>
> user-java@ibatis.apache.org
>
>   To
> <us...@ibatis.apache.org>
>  cc
>
>  Subject
> RE: Optimistic locking question
>
>
>
>
>
>
> Instead of using a timestamp I use an integer and increment the value on
> every update.
>
> update customer
> set NAME= #name#,
>     *VERSION_ID = VERSION_ID + 1*
> where ID = #ID# and *VERSION_ID  = #VERSION_ID#*
>
> Then in the Dao I would increment versionId value on the CustomerDTO
> object if the update succeeded.
>         int rowCount = getSqlMapClientTemplate().update(
> "Customer-update", record);
>
>        if(rowCount == 0) {
>            throw new OptimisticLockingFailureException("Optimistic Lock
> Failure! Unable to update record: " + record);
>        }
>         *record.setVersionId(record.getVersionId() + 1 );*
>
>
> ------------------------------
> *From:* Christopher.Mathrusse@sybase.com [mailto:
> Christopher.Mathrusse@sybase.com] *
> Sent:* Wednesday, 15 March 2006 11:55 PM*
> To:* user-java@ibatis.apache.org*
> Subject:* Optimistic locking question
>
>
> I have a table:
>
> CREATE TABLE CUSTOMER
>        ID NUMBER NOT NULL,
>        NAME VARCHAR(32) NOT NULL,
>        RECORD_DATE TIMESTAMP NOT NULL,
>        ...
>
> 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:
>
>   update customer set
>     name = #name#,
>     record_date = getDate(),
>     ...
>   where
>     id = #id#
>   and
>     record_date = #recordDate#
>
> This allows me to do the following in my Dao:
>
>        int rowCount = getSqlMapClientTemplate().update( "Customer-update",
> record);
>
>        if(rowCount == 0) {
>            throw new OptimisticLockingFailureException("Optimistic Lock
> Failure! Unable to update record: " + record);
>        }
>
>        return rowCount;
>
>
> 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?
>
>
> Thanks for the help...
>
> Chris Mathrusse
> christopher.mathrusse@sybase.com
> Sybase, Inc
> One Sybase Drive
> Dublin, CA 94568
> (925) 236-5553  "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.
>
> 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.
>
>