aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/transactions.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-30 14:51:04 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-30 14:51:04 +0000
commit50562c2a7e1a82c7a663889a38f7dad6bbcb6750 (patch)
treeacf492f758037ce2d7e574faf0b45faa627402ab /activerecord/lib/active_record/transactions.rb
parent3be52ef47cd9ee7f40956deaab6093fa3cd80036 (diff)
downloadrails-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/lib/active_record/transactions.rb')
-rw-r--r--activerecord/lib/active_record/transactions.rb26
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)