When coordinating updates from multiple database sessions, optimistic locking is a strategy that assumes all updates can complete without conflict. It does not hold locks on any records while the user is editing, but checks to see if any other edits have occurred when the user tries to commit the changes. If two sessions try to edit the same data, the second one to commit will be rejected and have to redo the change. Also see pessimistic locking.
Questions tagged [optimistic-locking]
422 questions
933
votes
13 answers
Optimistic vs. Pessimistic locking
I understand the differences between optimistic and pessimistic locking. Now, could someone explain to me when I would use either one in general?
And does the answer to this question change depending on whether or not I'm using a stored procedure…

Jason Baker
- 192,085
- 135
- 376
- 510
39
votes
2 answers
Optimistic Locking by concrete (Java) example
I have spent my morning reading all the top articles that Google churns up on optimistic locking, and for the life of me, I still don't really get it.
I understand that optimistic locking involves the addition of a column for tracking the record's…

AdjustingForInflation
- 1,571
- 2
- 26
- 50
34
votes
4 answers
Optimistic locking in a stateless application with JPA / Hibernate
I'm wondering what would be the best way to implement optimistic locking (optimistic concurrency control) in a system where entity instances with a certain version can not be kept between requests. This is actually a pretty common scenario but…

deamon
- 89,107
- 111
- 320
- 448
28
votes
2 answers
How are locking mechanisms (Pessimistic/Optimistic) related to database transaction isolation levels?
I am writing a web application where two different users can update a list of things, to do list, for example. I have come to realize that, optimistic locking mechanism works best since I don't expect high contention.
I was looking at transaction…

Abhishek Shukla Ravishankara
- 613
- 10
- 17
27
votes
1 answer
Which HTTP status code to use to reject a PUT due to optimistic locking failure
Assume I'd like to implement some kind of optimistic locking and use ETags to indicate the most up to date resource state. This means, clients will use an If-Match header when PUTting for an update.
According to the HTTP spec, the server has to…

Oliver Drotbohm
- 80,157
- 18
- 225
- 211
22
votes
4 answers
Spring Optimistic Locking:How to retry transactional method till commit is successful
I use Spring 2.5 and Hibernate JPA implementation with Java and "container" managed Transactions.
I have a "after user commit" method that updates data in background and need to be committed regardless of ConcurrencyFailureException or…

knarf1983
- 313
- 1
- 2
- 9
22
votes
1 answer
How to handle JPA ObjectOptimisticLockException properly for multiple simultaneous transaction requests?
So, I was working on a simple Spring MVC + JPA (hibernate) project where there are Users who can makes Posts and make Comments on their friends Posts (somewhat like a small social network) . I am still relatively new using JPA Hibernate. So, when I…

adn.911
- 1,256
- 3
- 17
- 30
22
votes
2 answers
Optimistic Locking in Hibernate by default
I have one question about optimistic locking in Hibernate. I am trying to go deep inside optimistic locking with Hibernate, but I have one doubt. Hibernate uses version approach (integer or timestamp) to implement optimistic locking. To configure…

lordofthejars
- 713
- 1
- 6
- 14
21
votes
2 answers
How do I avoid a race condition in my Rails app?
I have a really simple Rails application that allows users to register their attendance on a set of courses. The ActiveRecord models are as follows:
class Course < ActiveRecord::Base
has_many :scheduled_runs
...
end
class ScheduledRun <…

Cathal
- 243
- 3
- 8
21
votes
6 answers
Java Solutions for Distributed Transactions and/or Data Shared in Cluster
What are the best approaches to clustering/distributing a Java server application ?
I'm looking for an approach that allows you to scale horizontally by adding more application servers, and more database servers.
What technologies (software…

Dougnukem
- 14,709
- 24
- 89
- 130
17
votes
2 answers
MongoDB Document Operations are Atomic and Isolated, but Are They Consistent?
I'm in the process of porting my application from an App Engine Datastore to a MongoDB backend and have a question regarding the consistency of "document updates." I understand that the updates on one document are all atomic and isolated, but is…

Eternal Rubyist
- 3,445
- 4
- 34
- 51
14
votes
2 answers
Does DynamoDB have locking by default?
I'm looking over the dynamo documentation and it looks like they have optimistic. I'm wondering if this is used by default or not.
From the documentation, it looks like you need to code up the java application to use the @DynamoDBVersionAttribute…

Kevin
- 3,209
- 9
- 39
- 53
13
votes
3 answers
Best type for JPA version field for Optimistic locking
I have doubts about which is the best type for a field annotated with @Version for optimistic locking in JPA.
The API javadoc (http://docs.oracle.com/javaee/7/api/javax/persistence/Version.html) says:
"The following types are supported for version…

Aliuk
- 1,249
- 2
- 17
- 32
12
votes
3 answers
Retry mechanism for optimistic locking (spring data + JPA)
We decided on using optimistic locking in our web application in order to increase concurrency and without the using of pessimistic locking.
We are now on a lookout for retry solutions.
We would like to have as little impact as possible to our…

Urbanleg
- 6,252
- 16
- 76
- 139
12
votes
1 answer
How to use the Hibernate optimistic locking version property on the front end?
Optimistic locking using the version attribute for an entity works fine and is easy to implement:
The entity has a property of the following type:
private int VERSION;
public int…

les2
- 14,093
- 16
- 59
- 76