Take the 2-minute tour ×

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?

share|improve this question

3 Answers 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)
  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)
      raise e
share|improve this answer
This just saved me a headache. Thank you. –  Sean McCleary Oct 3 '12 at 2:50

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.

share|improve this answer

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

share|improve this answer
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? –  digger69 May 14 at 22:25

Your Answer


