public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Move the transaction counter to the connection object rather than 
maintaining it on the current Thread.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#533 state:resolved]
jviney (author)
Tue Jul 01 21:01:26 -0700 2008
NZKoz (committer)
Tue Jul 15 12:25:12 -0700 2008
commit  4f72feb84c25b54f66c7192c788b7fd965f2d493
tree    caa8cf9cb15c786dc627e438122da822986d8f6e
parent  24a8ae4e08fcd15a8c3792990d1d0981d004d339
...
118
119
120
 
 
 
 
 
 
 
 
 
 
 
 
 
121
122
123
...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
0
@@ -118,6 +118,19 @@ module ActiveRecord
0
         @connection
0
       end
0
 
0
+ def open_transactions
0
+ @open_transactions ||= 0
0
+ end
0
+
0
+ def increment_open_transactions
0
+ @open_transactions ||= 0
0
+ @open_transactions += 1
0
+ end
0
+
0
+ def decrement_open_transactions
0
+ @open_transactions -= 1
0
+ end
0
+
0
       def log_info(sql, name, runtime)
0
         if @logger && @logger.debug?
0
           name = "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})"
...
515
516
517
518
 
519
520
521
...
930
931
932
933
 
934
935
936
...
951
952
953
954
 
955
956
 
957
958
959
...
515
516
517
 
518
519
520
521
...
930
931
932
 
933
934
935
936
...
951
952
953
 
954
955
 
956
957
958
959
0
@@ -515,7 +515,7 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
0
 
0
           all_loaded_fixtures.update(fixtures_map)
0
 
0
- connection.transaction(Thread.current['open_transactions'].to_i == 0) do
0
+ connection.transaction(connection.open_transactions.zero?) do
0
             fixtures.reverse.each { |fixture| fixture.delete_existing_fixtures }
0
             fixtures.each { |fixture| fixture.insert_fixtures }
0
 
0
@@ -930,7 +930,7 @@ module Test #:nodoc:
0
             load_fixtures
0
             @@already_loaded_fixtures[self.class] = @loaded_fixtures
0
           end
0
- ActiveRecord::Base.send :increment_open_transactions
0
+ ActiveRecord::Base.connection.increment_open_transactions
0
           ActiveRecord::Base.connection.begin_db_transaction
0
         # Load fixtures for every test.
0
         else
0
@@ -951,9 +951,9 @@ module Test #:nodoc:
0
         end
0
 
0
         # Rollback changes if a transaction is active.
0
- if use_transactional_fixtures? && Thread.current['open_transactions'] != 0
0
+ if use_transactional_fixtures? && ActiveRecord::Base.connection.open_transactions != 0
0
           ActiveRecord::Base.connection.rollback_db_transaction
0
- Thread.current['open_transactions'] = 0
0
+ ActiveRecord::Base.connection.decrement_open_transactions
0
         end
0
         ActiveRecord::Base.verify_active_connections!
0
       end
...
73
74
75
76
 
77
78
79
 
80
81
 
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
...
73
74
75
 
76
77
78
 
79
80
 
81
82
83
 
 
 
 
 
 
 
 
 
 
 
84
85
86
0
@@ -73,25 +73,14 @@ module ActiveRecord
0
     # trigger a ROLLBACK when raised, but not be re-raised by the transaction block.
0
     module ClassMethods
0
       def transaction(&block)
0
- increment_open_transactions
0
+ connection.increment_open_transactions
0
 
0
         begin
0
- connection.transaction(Thread.current['start_db_transaction'], &block)
0
+ connection.transaction(connection.open_transactions == 1, &block)
0
         ensure
0
- decrement_open_transactions
0
+ connection.decrement_open_transactions
0
         end
0
       end
0
-
0
- private
0
- def increment_open_transactions #:nodoc:
0
- open = Thread.current['open_transactions'] ||= 0
0
- Thread.current['start_db_transaction'] = open.zero?
0
- Thread.current['open_transactions'] = open + 1
0
- end
0
-
0
- def decrement_open_transactions #:nodoc:
0
- Thread.current['open_transactions'] -= 1
0
- end
0
     end
0
 
0
     def transaction(&block)
...
461
462
463
464
 
465
466
 
467
468
 
469
470
471
...
461
462
463
 
464
465
 
466
467
 
468
469
470
471
0
@@ -461,11 +461,11 @@ class FixturesBrokenRollbackTest < ActiveRecord::TestCase
0
   alias_method :teardown, :blank_teardown
0
 
0
   def test_no_rollback_in_teardown_unless_transaction_active
0
- assert_equal 0, Thread.current['open_transactions']
0
+ assert_equal 0, ActiveRecord::Base.connection.open_transactions
0
     assert_raise(RuntimeError) { ar_setup_fixtures }
0
- assert_equal 0, Thread.current['open_transactions']
0
+ assert_equal 0, ActiveRecord::Base.connection.open_transactions
0
     assert_nothing_raised { ar_teardown_fixtures }
0
- assert_equal 0, Thread.current['open_transactions']
0
+ assert_equal 0, ActiveRecord::Base.connection.open_transactions
0
   end
0
 
0
   private
...
57
58
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
0
@@ -57,4 +57,29 @@ class MultipleDbTest < ActiveRecord::TestCase
0
 
0
     assert Course.connection
0
   end
0
+
0
+ def test_transactions_across_databases
0
+ c1 = Course.find(1)
0
+ e1 = Entrant.find(1)
0
+
0
+ begin
0
+ Course.transaction do
0
+ Entrant.transaction do
0
+ c1.name = "Typo"
0
+ e1.name = "Typo"
0
+ c1.save
0
+ e1.save
0
+ raise "No I messed up."
0
+ end
0
+ end
0
+ rescue
0
+ # Yup caught it
0
+ end
0
+
0
+ assert_equal "Typo", c1.name
0
+ assert_equal "Typo", e1.name
0
+
0
+ assert_equal "Ruby Development", Course.find(1).name
0
+ assert_equal "Ruby Developer", Entrant.find(1).name
0
+ end
0
 end

Comments

    No one has commented yet.