Optimistic locking
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
detect concurrent edits with a version column.
Planned topics
- Add
@Version long versionto the entity; Hibernate addsWHERE id = ? AND version = ?to every update and increments the version. - If zero rows are updated, it throws
OptimisticLockException(Spring:ObjectOptimisticLockingFailureException). - No database lock is held while the user thinks; it suits low-conflict workloads.
- Expose the version to clients (ETag or a field) so edits across requests are protected too.
- Map the exception to HTTP 409 with the current state.
Draft outline details
Demonstration
@Entity class Product {
@Id Long id;
@Version long version;
String name;
}Check your understanding
What does Hibernate add to the update statement?
Show answer
A check that the version is still the one that was read.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.