Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10212: mutil_db_transaction_with_tests.txt

File mutil_db_transaction_with_tests.txt, 3.0 kB (added by delwaterman, 2 years ago)

Full patch with Tests

Line 
1 Index: C:/code_work/Edge Rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
2 ===================================================================
3 --- C:/code_work/Edge Rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb      (revision 8167)
4 +++ C:/code_work/Edge Rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb      (working copy)
5 @@ -117,6 +117,23 @@
6          @connection
7        end
8  
9 +      # Used to track the number of transactions currently running
10 +      # on a connection.
11 +      def open_transactions
12 +        @open_transactions ||= 0
13 +      end
14 +
15 +      # Increment the number of database transactions running by one.
16 +      def increment_open_transactions
17 +        @open_transactions ||= 0
18 +        @open_transactions += 1
19 +      end
20 +     
21 +      # Decrement the number of database transactions running by one.
22 +      def decrement_open_transactions
23 +        @open_transactions -= 1
24 +      end
25 +
26        def log_info(sql, name, runtime)
27          if @logger && @logger.debug?
28            name = "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})"
29 Index: C:/code_work/Edge Rails/activerecord/lib/active_record/transactions.rb
30 ===================================================================
31 --- C:/code_work/Edge Rails/activerecord/lib/active_record/transactions.rb      (revision 8167)
32 +++ C:/code_work/Edge Rails/activerecord/lib/active_record/transactions.rb      (working copy)
33 @@ -84,12 +84,13 @@
34      module ClassMethods
35        def transaction(&block)
36          previous_handler = trap('TERM') { raise TransactionError, "Transaction aborted" }
37 -        increment_open_transactions
38 +        trans_count = connection.increment_open_transactions
39 +        start_db_trans = (trans_count == 1)
40  
41          begin
42 -          connection.transaction(Thread.current['start_db_transaction'], &block)
43 +          connection.transaction(start_db_trans, &block)
44          ensure
45 -          decrement_open_transactions
46 +          connection.decrement_open_transactions
47            trap('TERM', previous_handler)
48          end
49        end
50 Index: C:/code_work/Edge Rails/activerecord/test/multiple_db_test.rb
51 ===================================================================
52 --- C:/code_work/Edge Rails/activerecord/test/multiple_db_test.rb       (revision 8167)
53 +++ C:/code_work/Edge Rails/activerecord/test/multiple_db_test.rb       (working copy)
54 @@ -57,4 +57,29 @@
55  
56      assert Course.connection
57    end
58
59 +  def test_transactions_across_databases
60 +    c1 = Course.find(1)
61 +    e1 = Entrant.find(1)
62 +   
63 +    begin
64 +      Course.transaction do
65 +        Entrant.transaction do
66 +          c1.name = "Typo"
67 +          e1.name = "Typo"
68 +          c1.save
69 +          e1.save
70 +          raise "No I messed up."
71 +        end
72 +      end
73 +    rescue
74 +      # Yup caught it
75 +    end
76 +   
77 +    assert_equal "Typo", c1.name
78 +    assert_equal "Typo", e1.name
79 +   
80 +    assert_equal "Ruby Development", Course.find(1).name
81 +    assert_equal "Ruby Developer", Entrant.find(1).name
82 +  end
83  end