锁定 MySQL 中的特定记录

发布于 2024-10-05 01:13:47 字数 384 浏览 10 评论 0原文

我即将实现一个记录锁定机制,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2024-10-12 01:13:48

您可以使用 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文