Module: TransactionRetry::ActiveRecord::Base::ClassMethods

Defined in:
lib/transaction_retry/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#transaction_with_retry(*objects, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/transaction_retry/active_record/base.rb', line 19

def transaction_with_retry(*objects, &block)
  retry_count = 0

  begin
    transaction_without_retry(*objects, &block)
  rescue ::ActiveRecord::TransactionIsolationConflict
    raise if retry_count >= TransactionRetry.max_retries
    raise if tr_in_nested_transaction?
    
    retry_count += 1
    postfix = { 1 => 'st', 2 => 'nd', 3 => 'rd' }[retry_count] || 'th'
    logger.warn "Transaction isolation conflict detected. Retrying for the #{retry_count}-#{postfix} time..." if logger
    tr_exponential_pause( retry_count )
    retry
  end
end