Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Restored thread safety to Active Record [andreas]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@285 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 50562c2a7e1a82c7a663889a38f7dad6bbcb6750 1 parent 3be52ef
David Heinemeier Hansson dhh authored
Showing with 16 additions and 10 deletions.
  1. +16 −10 activerecord/lib/active_record/transactions.rb
26 activerecord/lib/active_record/transactions.rb
View
@@ -76,14 +76,10 @@ def self.append_features(base)
# should be ready to catch those in your application code.
#
# Tribute: Object-level transactions are implemented by Transaction::Simple by Austin Ziegler.
- module ClassMethods
+ module ClassMethods
def transaction(*objects, &block)
- TRANSACTION_MUTEX.synchronize do
- Thread.current['open_transactions'] ||= 0
- Thread.current['start_db_transaction'] = (Thread.current['open_transactions'] == 0)
- Thread.current['open_transactions'] += 1
- end
-
+ lock_mutex
+
begin
objects.each { |o| o.extend(Transaction::Simple) }
objects.each { |o| o.start_transaction }
@@ -96,11 +92,21 @@ def transaction(*objects, &block)
objects.each { |o| o.abort_transaction }
raise
ensure
- TRANSACTION_MUTEX.synchronize do
- Thread.current['open_transactions'] -= 1
- end
+ unlock_mutex
end
end
+
+ def lock_mutex
+ Thread.current['open_transactions'] ||= 0
+ TRANSACTION_MUTEX.lock if Thread.current['open_transactions'] == 0
+ Thread.current['start_db_transaction'] = (Thread.current['open_transactions'] == 0)
+ Thread.current['open_transactions'] += 1
+ end
+
+ def unlock_mutex
+ Thread.current['open_transactions'] -= 1
+ TRANSACTION_MUTEX.unlock if Thread.current['open_transactions'] == 0
+ end
end
def transaction(*objects, &block)
Please sign in to comment.
Something went wrong with that request. Please try again.