Reputation: 20096
We have an entity class that uses @Version
.
@Version
protected Long auditVersion;
What if there are two threads both trying to insert the first occurrence of the entity? Both instances of the entity will have auditVersion
set to null
. It seems that Hibernate does not take any notice of the optimistic locking fail, I do not get the StaleObjectStateException
I expected.
Why not?
Upvotes: 2
Views: 809
Reputation: 691635
Because optimistic locking is used to handle concurrent updates and removals. If two threads try to insert two entities with the same ID, the unique constraint on the primary key is sufficient to detect the conflict. No need for optimistic locking for this.
Upvotes: 4