A and B access a set of data on the same page at the same time. A has modified one of the data. B's page has not been refreshed, so the information B sees is before A's modification. At this time, B also needs to modify this piece of data. How can I remind B that this piece of data has been modified.
I saw a way to find this data based on the last modified time before modifying the data (select * from xxx where updatetime = xxx and id= xxx). If it cannot be found, it means it has been modified. Check The description has not been modified when it arrives.
I would like to ask if there is a better way?
I remember that optimistic locking in hibernate is implemented using the version number field. After each update is successful, the value of the version number field is incremented by 1.
Before updating, first check whether the version number in the database and the version number saved in the page are the same. , if the version number becomes larger, the user is prompted that other users have modified the data during editing.
Of course, you can also use pessimistic locking. When entering the page, use select... for update to lock the record. At this time, other users cannot edit the locked record at the same time.
I will do this - add a field, edit_num, +1 for each change, and compare it when B needs to be changed.
Optimistic Lock Optimistic Lock Optimistic Lock
Optimistic locking mechanism is implemented by adding a version field to the data
Add a field to the data table
update_at
The type isTIMESATMP
, the default value isCURRENT_TIMESTAMP
, no other changes are neededWhenever the data is updated, the value will be updated automatically
Add a version field to this data. The version number starts from 0 by default. It will increment automatically if it is not modified. Just verify whether this version is consistent with the version in the database each time it is submitted.
Add a version number field to the xxx table