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
7
votes
3 answers
Could there be a deadlock when using optimistic locking?
As is known, there are two locking strategy: Optimistic vs. Pessimistic locking
Pessimistic Locking is when you lock the record for your exclusive use
until you have finished with it. It has much better integrity than
optimistic locking but…

Alex
- 12,578
- 15
- 99
- 195
7
votes
1 answer
HibernateOptimisticLockingFailureException marks connection as 'closed'?
I'm getting the following stack trace:
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.btfin.wrapcore.request.MFRequest] with identifier [2850448]: optimistic locking failed; nested exception is…

hawkeye
- 34,745
- 30
- 150
- 304
7
votes
1 answer
JPA: How does Read Lock work?
I am trying to understand whats the effect of calling EntityManager.lock(entity, LockModeType.READ). The API documentation sounds very confusing for me.
If I have to concurrent threads and Thread 1 calls lock(entity, LockModeType.READ), can Thread 2…

rubberduck
- 71
- 1
- 2
6
votes
2 answers
Using ETag for optimistic locking in a Django REST application
I'm trying to select a REST framework for Django that will allow me to easily use ETags for optimistic locking. I'm planning on examining Django-pistons and the Django Rest Framework libraries, but I'm open to any non-GPL solution (corporate…

Chris R
- 17,546
- 23
- 105
- 172
6
votes
1 answer
Hibernate: Should I include the 'version' field to hashcode() and equals() methods
I know that when overriding hashcode() and equals() of my persistent entities I should not include ID and only include the meaningful properties that uniquely identify the object.
But what about version field which is used for the optimistic…

Alex Vayda
- 6,154
- 5
- 34
- 50
6
votes
1 answer
Optimistic concurrency on multi-table complex entity
I have a complex entity (let's call it Thing) which is represented in SQL Server as many tables: one parent table dbo.Thing with several child tables dbo.ThingBodyPart, dbo.ThingThought, etc. We've implemented optimistic concurrency using a single…

Daniel Schilling
- 4,829
- 28
- 60
6
votes
0 answers
Optimistic and pessimistic locking in Node/Postgres?
I'm planning to use Node for my next project, because it seems to be a good fit for the problem I'm solving.
One thing I need to figure out is optimistic and pessimistic locking.…

randomguy
- 12,042
- 16
- 71
- 101
5
votes
3 answers
Why does activerecord optimistic locking work only once per row?
Somehow, I always get these on Fridays.
My earlier question was regarding the same problem, but I can now narrow things down a bit:
I've been playing with this all day, trying to make sense of it. I have a table with a lock_version colum, specified…

Sniggerfardimungus
- 11,583
- 10
- 52
- 97
5
votes
0 answers
JPA Optimistic Locking: How to prevent deleting if record has been updated and vice versa?
I need my app to have the following behavior:
Scenario 1
User A views an order.
User B views the same order.
User A deletes the order.
User B requests to update the order. This should fail.
Scenario 2
User A views an order
User B views the same…

user8297969
- 415
- 1
- 6
- 18
5
votes
1 answer
How does Hibernate do row version check for Optimistic Locking before committing the transaction
When before committing the current transaction hibernate checks the version of the row, it should issue an sql select statement for fetching ithe row.
Assume that after issuing that select statement hibernate finds out that the row version is not…

Suren Aznauryan
- 984
- 10
- 24
5
votes
1 answer
Hibernate unexpectedly issues INSERT instead of throwing the javax.persistence.OptimisticLockException, when a nonexistent entity is passed to merge()
A client application supplies a stale entity which is to be merged by Hibernate. Taking a very simple example.
public Entity update(Entity entity) {
return entityManager.contains(entity) ? entity : entityManager.merge(entity);
}
Entity is a…

Tiny
- 27,221
- 105
- 339
- 599
5
votes
1 answer
Optimistic locking while deleting entities in JPA
A transactional method in an EJB using CMT that removes an an entity supplied :
public boolean delete(Entity entity) {
Entity managedEntity = entityManager.find(Entity.class, entity.getId());
if (managedEntity == null) {
throw new…

Tiny
- 27,221
- 105
- 339
- 599
5
votes
1 answer
Rails - Optimistic locking always fires StaleObjectError exception
I'm learning rails, And read about optimistic lock. I've added lock_version column of type integer into my articles table.
But now whenever I try to update a record for the first time, I get StaleObjectError exception.
Here's my migration:
class…

Rafael Adel
- 7,673
- 25
- 77
- 118
5
votes
2 answers
Grails 2.3.7 Optimistic Locking version is updated every time a Command Object is submitted
I have the following
def save(ACommand command){
...
}
@Validateable
class ACommand implements Serializable
{
ADomainObject bundleDef
}
but every time save is called the version is incremented. So if I open up two browsers and submit a…

Jackie
- 21,969
- 32
- 147
- 289
5
votes
2 answers
can ZooKeeper get znode data and znode data version (stat) in one single operation?
I am developing an application that use ZooKeeper as the datastore. For one of the methods in the application, I need to use the optimistic concurrent control. For example, I need to implement a get method which get the znode data, and I use the…

jedli2006
- 51
- 1
- 3