MySQL Race Conditions

后端 未结 4 901
半阙折子戏
半阙折子戏 2021-01-07 06:15

Does this cause a race condition with MySQL (InnoDB):

  1. Start Transaction.

  2. Try to get record.

  3. If record doesn\'t exist, return

4条回答
  •  悲哀的现实
    2021-01-07 06:38

    Yes, it's possible for another transaction to check the table after you've read it.

    Worse yet, because of how transactions work, even after you delete the row, any new transactions that start will see the row because you haven't yet committed the delete.

    SELECT ... FOR UPDATE is one way to prevent it.

    LOCK TABLE tablename is another.

    Unfortunately, since you're using an ORM, I couldn't say whether it has the ability to do either of these.

提交回复
热议问题