Class: ApplicationRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
NilifyBlanks
Defined in:
app/models/application_record.rb

Class Method Summary collapse

Class Method Details

.transaction_with_retry(&block) ⇒ Object

Will run block on transaction, repeating 3 times if failed due to ActiveRecord:DeadLock exception.


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/application_record.rb', line 13

def self.transaction_with_retry(&block)
  retry_count = 0
  begin
    transaction &block
  rescue ActiveRecord::Deadlocked
    raise if retry_count > 3
    # Exponential backoff with proportional random wait time.
    sleep 2**(retry_count)*(1+rand)

    retry_count += 1
    retry
  end
end