I have a question related to optimistic locking in Spring/Hibernate actually.
I have following scenario with typical REST application with SQL database.
- User A enters page and reads data - version 0 of entity - GET request
- User B enters page and reads data - version 0 of entity - GET request
- User A saves data - version 1 of entity - PUT request
- User B wants to save data (PUT request), but I should see optimistic lock exception
Now my question: Where hibernate saves data about entity version? I understand the situation when everything is in the same transaction:
- Load data
- Someone changed entity in the different transaction
- Save data
But in my situation version will vanish GET and PUT are in totally different transaction/threads etc.
In my opinion I should save somewhere version loaded by the user to have correlation between GET and PUT requests e.g. in HTTP session or just return version in the response and then send that version in the PUT request.
Can it be done in the better way? Like out of the box?