ActiveRecord3死锁重试 [英] ActiveRecord3 deadlock retry

查看:62
本文介绍了ActiveRecord3死锁重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于Rails 3(或ActiveRecord 3)的插件复制旧的 deadlock_retry 插件?或者,该插件仍可与Rails 3一起使用吗?

Are there any plugins for Rails 3 (or ActiveRecord 3) that replicate the old deadlock_retry 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

这篇关于ActiveRecord3死锁重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆