I have implemented Optimistic Locking; it works in one environment and not in other.
In my test case, I load 2 objects using the same id. Initially the version number in the database is 0. I make changes to both first and second object. I saveOrUpdate the first object it works fine, and the version number is updated to 1. When I saveOrUpdate the second Object, I get StaleObjectStateException. It works perfectly.
The problem is in the web environment, I open the same page on two machines. The pages load the same Object. Initially the version number is 0. I make changes to both the pages. I save the first page, it saves and the version number is updated to 1. Now, I save the second page. I excepted a StaleObjectStateException. But I don't get the error. The second save overrides my first change. The version number is also updated to 2.
Does this situation call for a manual check in the version number before the saveOrUpdate? How is the second save able to override the first save and even update the version number?
Please provide your input.
Thanks.
|