Demo: concurrent edits with @Version
On This Lesson 3 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
show two admins editing the same product and returning 409.
Draft outline details
Step by step
- Add
@VersiontoProductand includeversionin the response DTO. - Two clients fetch product 1 (version 3).
- Client A updates the name with version 3 and succeeds (version 4).
- Planned mistake: the update DTO omits the version, so the entity is loaded fresh and the conflict is missed. Fix: require the version in the request and compare it.
- Client B updates with version 3:
ObjectOptimisticLockingFailureException, mapped by@ControllerAdviceto 409 with the current version. - Write an integration test for the two-client conflict.
Prepares MID-016-LAB1.
Check your understanding
What must the client send back to make optimistic locking work across requests?
Show answer
The version it 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.