search

Home  >  Q&A  >  body text

php - How does the database determine whether a piece of data has been modified?

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?

大家讲道理大家讲道理2699 days ago1589

reply all(7)I'll reply

  • typecho

    typecho2017-07-07 10:37:07

    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.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-07-07 10:37:07

    I will do this - add a field, edit_num, +1 for each change, and compare it when B needs to be changed.

    reply
    0
  • 黄舟

    黄舟2017-07-07 10:37:07

    Optimistic Lock Optimistic Lock Optimistic Lock

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-07 10:37:07

    Optimistic locking mechanism is implemented by adding a version field to the data

    reply
    0
  • 大家讲道理

    大家讲道理2017-07-07 10:37:07

    Add a field to the data tableupdate_atThe type is TIMESATMP, the default value is CURRENT_TIMESTAMP, no other changes are needed
    Whenever the data is updated, the value will be updated automatically

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-07 10:37:07

    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.

    reply
    0
  • 为情所困

    为情所困2017-07-07 10:37:07

    Add a version number field to the xxx table

    reply
    0
  • Cancelreply