Module: ActsAsOrderedTree::TenaciousTransaction

Included in:
InstanceMethods
Defined in:
lib/acts_as_ordered_tree/tenacious_transaction.rb

Constant Summary collapse

DEADLOCK_MESSAGES =
/Deadlock found when trying to get lock|Lock wait timeout exceeded|deadlock detected/.freeze
RETRY_COUNT =
10

Instance Method Summary collapse

Instance Method Details

#tenacious_transaction(&block) ⇒ Object

Partially borrowed from awesome_nested_set



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/acts_as_ordered_tree/tenacious_transaction.rb', line 7

def tenacious_transaction(&block) #:nodoc:
  return transaction(&block) if @in_tenacious_transaction

  @in_tenacious_transaction = true
  retry_count = 0
  begin
    transaction(&block)
  rescue ActiveRecord::StatementInvalid => error
    raise unless self.class.connection.open_transactions.zero?
    raise unless error.message =~ DEADLOCK_MESSAGES
    raise unless retry_count < RETRY_COUNT
    retry_count += 1

    logger.info "Deadlock detected on retry #{retry_count}, restarting transaction"

    sleep(rand(retry_count)*0.1) # Aloha protocol

    retry
  ensure
    @in_tenacious_transaction = false
  end
end