Skip to content

Commit

Permalink
Merge pull request collectiveidea#302 from marutosi/master-merge-2-1-…
Browse files Browse the repository at this point in the history
…stable

merge 2-1-stable to master
  • Loading branch information
parndt committed Mar 3, 2015
2 parents fbd7a12 + a9749cd commit 4f47f8c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/awesome_nested_set/model/transactable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ module Acts #:nodoc:
module NestedSet #:nodoc:
module Model
module Transactable
class OpenTransactionsIsNotZero < ActiveRecord::StatementInvalid
end

class DeadlockDetected < ActiveRecord::StatementInvalid
end

protected
def in_tenacious_transaction(&block)
retry_count = 0
begin
transaction(&block)
rescue CollectiveIdea::Acts::NestedSet::Move::ImpossibleMove
raise
rescue ActiveRecord::StatementInvalid => error
raise unless self.class.connection.open_transactions.zero?
raise OpenTransactionsIsNotZero.new(error.message) unless connection.open_transactions.zero?
raise unless error.message =~ /[Dd]eadlock|Lock wait timeout exceeded/
raise unless retry_count < 10
raise DeadlockDetected.new(error.message) unless retry_count < 10
retry_count += 1
logger.info "Deadlock detected on retry #{retry_count}, restarting transaction"
sleep(rand(retry_count)*0.1) # Aloha protocol
Expand Down
5 changes: 4 additions & 1 deletion lib/awesome_nested_set/move.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ def get_boundaries
[bound, other_bound]
end

class ImpossibleMove < ActiveRecord::StatementInvalid
end

def prevent_impossible_move
if !root && !instance.move_possible?(target)
raise ActiveRecord::ActiveRecordError, "Impossible move, target node cannot be inside moved tree."
raise ImpossibleMove, "Impossible move, target node cannot be inside moved tree."
end
end

Expand Down

0 comments on commit 4f47f8c

Please sign in to comment.