From 50562c2a7e1a82c7a663889a38f7dad6bbcb6750 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 30 Dec 2004 14:51:04 +0000 Subject: Restored thread safety to Active Record [andreas] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@285 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/transactions.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 74cce2b992..3deaef6866 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -76,14 +76,10 @@ module ActiveRecord # 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 @@ module ActiveRecord 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) -- cgit v1.2.3