开发者

ActiveRecord3 deadlock retry

Are there any plugins for Rails 3 (or ActiveRecord 3) that replicate the old deadlock_re开发者_运维技巧try plugin? Or, does that plugin still work with Rails 3?


I didn't even know there was a plugin to do this :)

Here's what we use (but you have to wrap deadlock-prone queries in it yourself):

# Executes the given block +retries+ times (or forever, if explicitly given nil),
# catching and retrying SQL Deadlock errors.
def retry_lock_error(retries = 100, &block)
  begin
    yield
  rescue ActiveRecord::StatementInvalid => e
    if e.message =~ /Deadlock found when trying to get lock/ and (retries.nil? || retries > 0)
      retry_lock_error(retries ? retries - 1 : nil, &block)
    else
      raise e
    end
  end
end


There is a transaction_retry gem which not only works with Rails 3+ but supports all major databases (MySQL, PostgreSQL and SQLite). It is marketed as clean and well tested.


rails / deadlock_retry

"Deadlock retry allows the database adapter (currently only tested with the MySQLAdapter) to retry transactions that fall into deadlock. It will retry such transactions three times before finally failing.

This capability is automatically added to ActiveRecord. No code changes or otherwise are required."

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜