bmc*_*mck 6 mysql innodb locking transactions ruby-on-rails
在我的Rails代码中,我需要确认只有在剩余某个记录超过1时才允许操作.出于这个原因,我需要锁定更新,然后执行读取.我的rails代码如下所示:
PaymentProfile.transaction do
profiles = PaymentProfile.lock("LOCK IN SHARE MODE").where(user_id: xxx)
if profiles.count > 1
#allow
else
#do not allow
end
end
Run Code Online (Sandbox Code Playgroud)
从理论上讲,这很有效并且可以正确锁定行.但是,如果另一个请求遍历相同的代码路径,则打开事务会删除我在另一个进程中取出的锁,从而破坏了锁的目的.
来自MySQL文档:
Beginning a transaction also causes table locks acquired with LOCK TABLES to be released, as though you had executed UNLOCK TABLES. Beginning a transaction does not release a global read lock acquired with FLUSH TABLES WITH READ LOCK.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1243 次 |
| 最近记录: |