How to properly avoid Mysql Race Conditions

前端 未结 3 653
既然无缘
既然无缘 2021-01-06 02:53

I know this has been asked before, but I\'m still confused and would like to avoid any problems before I go into programming if possible.

I plan on having an interna

3条回答
  •  一个人的身影
    2021-01-06 03:40

    To achieve this, you will need to lock the record somehow. Add a column LockedBy defaulting to 0.

    When someone pushes the button execute a query resembling this:

    UPDATE table SET LockedBy= WHERE LockedBy=0 and id=;

    After the update verify the affected rows (in php mysql_affected_rows). If the value is 0 it means the query did not update anything because the LockedBy column is not 0 and thus locked by someone else.

    Hope this helps

提交回复
热议问题