MySQL竞态条件 [英] MySQL Race Conditions

查看:146
本文介绍了MySQL竞态条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是否会导致MySQL(InnoDB)出现竞争状态:


  1. 开始交易。

  2. >
  3. 如果记录存在,请将其删除并添加一条记录被删除的日志条目。


  4. / rollback)。


另一个进程可能在2b中的删除步骤之前启动,的记录,然后让两个过程输入项目删除条目到日志中?



我需要采取什么预防措施吗?



感谢。

解决方案

2.只有一个进程能够在该行上获得锁定,从而避免您描述的情况。


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. If record exists, delete it and add a log entry saying that is was deleted.

  5. End Transaction (commit/rollback).

Is it possible for another process to start just before the delete step in 2b, detect the presence of the record and then have both processes enter item delete entries into the log?

Are there any precautions that I need to take?

Thanks.

解决方案

Use 'select for update' at step 2. Only one process will be able to get a lock on the row thus avoiding the scenario you described.

这篇关于MySQL竞态条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆