Skip to content
Ameen Abozaid
Planned lessonMID-016-L01

Optimistic locking

  • Lesson 1 of 7
  • Mid-level
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 version to the entity; Hibernate adds WHERE 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

java
@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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.