开发者

locking a specific record in MySQL

开发者 https://www.devze.com 2023-01-28 13:34 出处:网络
I am about to implement a record locking mechanism as follows: Table1: Field: \"ID\" (AutoIncrement, Primary)

I am about to implement a record locking mechanism as follows:

Table1:
    Field: "ID" (AutoIncrement, Primary)
    Field: "Name" (Text)
    Field: "More fields..."

Table2:
    Fiel开发者_运维问答d: "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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号