-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Optimistic Locking
PostPosted: Wed Aug 27, 2008 10:44 am 
Newbie

Joined: Tue Jul 22, 2008 10:46 pm
Posts: 6
TABLE
-----------------------------------------------------------------------------
| COLUMNA | COLUMNB | COLUMNC | TIMESTAMP |
|---------------------------------------------------------------------------
| A1 | B1 | C1 | T1 |
|----------------------------------------------------------------------------
| A2 | B2 | C2 | T2 |
-----------------------------------------------------------------------------



I am using Optimistic Locking Timestamp,All Strategy.In the above table


1.User 1 fetch the record based on where condition 'B1'

2.User 2 fetch the record based on where condition 'C1'

3.User 2 modify the column 'B1' update the record based on where condition 'C1'

4.User 1 modify the column 'C1' update the record based on where condition 'B1'


Question
--------

Does User 1 can able to update the record based on where condition 'B1' after User 2 updated the data ?


If answer is 'Yes' then what is the reason we are using Locking Strategy?

If answer is 'No' what is the difference between Pessimistic and Optimistic Locking?

What is the possible solution?. I am expecting answer from Hibernate technology.


Please let me know.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 11:47 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
We are using the version-based strategy but I think it should behave the same.

Quote:
Does User 1 can able to update the record based on where condition 'B1' after User 2 updated the data ?


No, User 1 will get a StaleStateException that indicates that another transaction has already modified the record.

Quote:
If answer is 'No' what is the difference between Pessimistic and Optimistic Locking?


When using pessimistic locking strategy the first user that is going to modify a record locks it when it is fetched from the database. In your case this would happen in step 1 and User 2 will not be able to get the same record in the second step. This will either result in User 2 having to wait for User 1 to release the lock or an exception is thrown.

Optimistic locking doesn't lock the record until the actual update is happening. This strategy is usually easier to use and performs better since locks are held for a shorter time.

Quote:
What is the possible solution?


What is the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 5:53 pm 
Newbie

Joined: Tue Jul 22, 2008 10:46 pm
Posts: 6
Thanks a Lot your response. I find out the solution.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.