锁定 MySQL 中的特定记录
我即将实现一个记录锁定机制,如下所示:
Table1:
Field: "ID" (AutoIncrement, Primary)
Field: "Name" (Text)
Field: "More fields..."
Table2:
Field: "ID" (Unique)
为了锁定 Table1 中的特定记录,我将在 Table2 中创建具有相同 ID 的记录。如果由于这样的记录已经存在而导致创建失败,则该记录必须被另一个进程锁定并且我旋转。如果成功,我就拥有锁并可以修改记录。完成后,我删除了 Table2 中的锁定记录。
我的问题是这是否是实现每记录锁的有效方法,如果不是,是否有这样的事情?
干杯, 查理.
I am about to implement a record locking mechanism as follows:
Table1:
Field: "ID" (AutoIncrement, Primary)
Field: "Name" (Text)
Field: "More fields..."
Table2:
Field: "ID" (Unique)
In order to lock a specific record in Table1, I'll create a record in Table2 with the same ID. If this creation fails due to such a record already existing, then the record must be locked by another process and I spin. If it succeeds, I have the lock and can modify the record. When I'm done, I delete the lock record in Table2.
My question is whether this is a valid way to implement a per-record lock, and if not, is there such a thing?
Cheers,
Charlie.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 innodb 进行基于行的锁定,
甚至读锁而不是强加锁定到另一张桌子,
(而不是让mysql来处理竞争条件)
You can use innodb for row-based locking,
or even read-lock instead of impose locking to another table,
(rather to let mysql to handle race-condition)