12

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?

Paul Richter
  • 10,908
  • 10
  • 52
  • 85
dunedain289
  • 2,348
  • 18
  • 21

3 Answers3

10

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
Martin T.
  • 3,132
  • 1
  • 30
  • 31
8

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.

qertoip
  • 1,870
  • 1
  • 17
  • 29
  • Hmm, last commit was June 2018, but prior to that it was June 2013. A 5 year window where nothing was done to the gem. I'm skeptical. Would love to hear if anybody has this solidly working in Rails 4+. – Joshua Pinter Jan 21 '19 at 04:56
  • We're trying it on our Rails 4.2.11 app right now and it appears to work great. It has completely resolved our Deadlock issues we've been seeing during intensive, concurrent imports and doesn't seem to be causing any other issues as far as we can tell. – Joshua Pinter Jan 21 '19 at 06:37
2

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."

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
rubyu2
  • 270
  • 1
  • 3
  • 14
  • re "This capability is automatically added to ActiveRecord" - trying to find source confirming this, google-fu failing me. Can you point me to a description of this in AR core? – Mark Nadig May 14 '14 at 22:25
  • 1
    I thin perhaps you misunderstood--- the gem referenced (rails / deadlock_retry) by the poster automatically adds this to active record – Jason FB Jul 21 '15 at 20:22