Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord3 deadlock retry

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?

like image 831
dunedain289 Avatar asked Oct 26 '10 20:10

dunedain289


3 Answers

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
like image 69
Martin T. Avatar answered Nov 19 '22 08:11

Martin T.


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.

like image 29
qertoip Avatar answered Nov 19 '22 10:11

qertoip


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

like image 2
rubyu2 Avatar answered Nov 19 '22 09:11

rubyu2