ActiveRecord::DeadlockRetry

Retries a transaction when a deadlock exception is returned from the database. Currently works with MySQL.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-deadlock_retry'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-deadlock_retry

Usage

ActiveRecord::DeadlockRetry.transaction do
  # Your possibly deadlocking code here
end

ActiveRecord::DeadlockRetry.transaction optionally takes an options hash with the following options:

  • :class - Class transaction (or :method) is called on. Defaults to ActiveRecord::Base.
  • :method - Method to call on :class (or ActiveRecord::Base). Defauls to transaction.
  • :retries - Number of retries. Defaults to 3.