Assuming I'm doing something like this (from the Active Record Querying guide)
假设我正在做这样的事情(来自Active Record Querying指南)
Item.transaction do
i = Item.first(:lock => true)
i.name = 'Jones'
i.save
end
Is the lock automatically released at the end of the transaction? I've looked at the Active Query guide and the ActiveRecord::Locking::Pessimistic docs, and couldn't find where it explicitly says where the lock is released.
是否在交易结束时自动释放锁定?我查看了Active Query指南和ActiveRecord :: Locking :: Pessimistic文档,但无法找到它明确说明锁定释放位置的位置。
3 个解决方案
#1
23
Locking is not a function of rails, it is just adding the lock statement to the query, which will vary depending on the database that you are using. Pessimistic Locking takes a "pessimistic" view in thinking that every query is subject to corruption. So it is going to lock selected rows until you are finished with the transaction. so Lock > query > unlock. While these are fairly consistent database to database, it might be good to read up on the database documentation that you using for any database-specific things you should know.
锁定不是rails的功能,它只是将lock语句添加到查询中,这将根据您使用的数据库而有所不同。悲观锁定在认为每个查询都受到腐败时采取“悲观”的观点。因此,它将锁定选定的行,直到您完成事务。所以锁定>查询>解锁。虽然这些数据库与数据库是相当一致的,但是阅读您用于任何您应该知道的特定于数据库的事物的数据库文档可能会很好。
Here is a good thread on optimistic vs. pessimistic locking that explains it better than I can. Optimistic vs. Pessimistic locking
这是一个关于乐观与悲观锁定的好线程,它比我能更好地解释它。乐观与悲观锁定
#2
5
Yes, the lock automatically released at the end of the transaction because this kind of lock is applicable to transactions only. It does not make sense to lock the record this way (pessimistic lock) outside the transaction.
是的,锁在事务结束时自动释放,因为这种锁仅适用于事务。在事务之外以这种方式锁定记录(悲观锁定)是没有意义的。
Pessimistic locks are enforced on DB level.
在数据库级别强制执行悲观锁定。
Below is a description with examples for mysql: http://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html
以下是mysql示例的说明:http://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html
#3
-2
I believe you'll want an "ensure" block to be certain the lock is released.
我相信你会想要一个“确保”块来确定锁被释放。
http://ruby-doc.org/core/classes/Mutex.src/M000916.html has:
http://ruby-doc.org/core/classes/Mutex.src/M000916.html有:
def synchronize
lock
begin
yield
ensure
unlock
end
end
http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/ seems to suggest, however, that the block structure of that method will automatically unlock.
但是,http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/似乎暗示该方法的块结构将自动解锁。
#1
23
Locking is not a function of rails, it is just adding the lock statement to the query, which will vary depending on the database that you are using. Pessimistic Locking takes a "pessimistic" view in thinking that every query is subject to corruption. So it is going to lock selected rows until you are finished with the transaction. so Lock > query > unlock. While these are fairly consistent database to database, it might be good to read up on the database documentation that you using for any database-specific things you should know.
锁定不是rails的功能,它只是将lock语句添加到查询中,这将根据您使用的数据库而有所不同。悲观锁定在认为每个查询都受到腐败时采取“悲观”的观点。因此,它将锁定选定的行,直到您完成事务。所以锁定>查询>解锁。虽然这些数据库与数据库是相当一致的,但是阅读您用于任何您应该知道的特定于数据库的事物的数据库文档可能会很好。
Here is a good thread on optimistic vs. pessimistic locking that explains it better than I can. Optimistic vs. Pessimistic locking
这是一个关于乐观与悲观锁定的好线程,它比我能更好地解释它。乐观与悲观锁定
#2
5
Yes, the lock automatically released at the end of the transaction because this kind of lock is applicable to transactions only. It does not make sense to lock the record this way (pessimistic lock) outside the transaction.
是的,锁在事务结束时自动释放,因为这种锁仅适用于事务。在事务之外以这种方式锁定记录(悲观锁定)是没有意义的。
Pessimistic locks are enforced on DB level.
在数据库级别强制执行悲观锁定。
Below is a description with examples for mysql: http://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html
以下是mysql示例的说明:http://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html
#3
-2
I believe you'll want an "ensure" block to be certain the lock is released.
我相信你会想要一个“确保”块来确定锁被释放。
http://ruby-doc.org/core/classes/Mutex.src/M000916.html has:
http://ruby-doc.org/core/classes/Mutex.src/M000916.html有:
def synchronize
lock
begin
yield
ensure
unlock
end
end
http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/ seems to suggest, however, that the block structure of that method will automatically unlock.
但是,http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/似乎暗示该方法的块结构将自动解锁。