Skip to content
This repository
Browse code

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 authored December 30, 2004
26  activerecord/lib/active_record/transactions.rb
@@ -76,14 +76,10 @@ def self.append_features(base)
76 76
     # should be ready to catch those in your application code.
77 77
     #
78 78
     # Tribute: Object-level transactions are implemented by Transaction::Simple by Austin Ziegler.
79  
-    module ClassMethods      
  79
+    module ClassMethods
80 80
       def transaction(*objects, &block)
81  
-        TRANSACTION_MUTEX.synchronize do
82  
-          Thread.current['open_transactions'] ||= 0
83  
-          Thread.current['start_db_transaction'] = (Thread.current['open_transactions'] == 0)
84  
-          Thread.current['open_transactions'] += 1
85  
-        end
86  
-
  81
+        lock_mutex
  82
+        
87 83
         begin
88 84
           objects.each { |o| o.extend(Transaction::Simple) }
89 85
           objects.each { |o| o.start_transaction }
@@ -96,11 +92,21 @@ def transaction(*objects, &block)
96 92
           objects.each { |o| o.abort_transaction }
97 93
           raise
98 94
         ensure
99  
-          TRANSACTION_MUTEX.synchronize do
100  
-            Thread.current['open_transactions'] -= 1
101  
-          end
  95
+          unlock_mutex
102 96
         end
103 97
       end
  98
+      
  99
+      def lock_mutex
  100
+        Thread.current['open_transactions'] ||= 0
  101
+        TRANSACTION_MUTEX.lock if Thread.current['open_transactions'] == 0
  102
+        Thread.current['start_db_transaction'] = (Thread.current['open_transactions'] == 0)
  103
+        Thread.current['open_transactions'] += 1
  104
+      end
  105
+      
  106
+      def unlock_mutex
  107
+        Thread.current['open_transactions'] -= 1
  108
+        TRANSACTION_MUTEX.unlock if Thread.current['open_transactions'] == 0
  109
+      end
104 110
     end
105 111
 
106 112
     def transaction(*objects, &block)

0 notes on commit 50562c2

Please sign in to comment.
Something went wrong with that request. Please try again.