diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-30 14:51:04 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-30 14:51:04 +0000 |
commit | 50562c2a7e1a82c7a663889a38f7dad6bbcb6750 (patch) | |
tree | acf492f758037ce2d7e574faf0b45faa627402ab /activerecord | |
parent | 3be52ef47cd9ee7f40956deaab6093fa3cd80036 (diff) | |
download | rails-50562c2a7e1a82c7a663889a38f7dad6bbcb6750.tar.gz rails-50562c2a7e1a82c7a663889a38f7dad6bbcb6750.tar.bz2 rails-50562c2a7e1a82c7a663889a38f7dad6bbcb6750.zip |
Restored thread safety to Active Record [andreas]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@285 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/transactions.rb | 26 |
1 files changed, 16 insertions, 10 deletions
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) |