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

Ticket #10212: transaction_fix.rb

File transaction_fix.rb, 1.6 kB (added by delwaterman, 11 months ago)

Monkey Patch of Transaction Issue.

Line 
1 # This file addresses the issue of Transaction counts that currently exist in ActiveRecoerd::Transactions
2
3 # Add a transaction counter to the Connection Object
4 class ActiveRecord::ConnectionAdapters::AbstractAdapter
5  
6   def increment_open_transactions
7     @open_transactions ||= 0
8     @open_transactions += 1
9   end
10  
11   def decrement_open_transactions
12     @open_transactions -= 1
13   end
14  
15   def open_transactions
16     @open_transactions ||= 0
17   end
18  
19 end
20
21 # Redefine this transaction method
22 module ActiveRecord::Transactions::ClassMethods
23   def transaction(*objects, &block)
24 #    raise "HELL"
25     previous_handler = trap('TERM') { raise TransactionError, "Transaction aborted" }
26     trans_count = connection.increment_open_transactions
27     puts "connection #{connection.instance_variable_get(:@config)[:database]} trans count: #{trans_count}"
28     start_db_trans = (trans_count == 1)
29     puts "connection #{connection.instance_variable_get(:@config)[:database]} start_db_trans #{start_db_trans}"
30
31     begin
32       unless objects.empty?
33         ActiveSupport::Deprecation.warn "Object transactions are deprecated and will be removed from Rails 2.0.  See http://www.rubyonrails.org/deprecation for details.", caller
34         objects.each { |o| o.extend(Transaction::Simple) }
35         objects.each { |o| o.start_transaction }
36       end
37      
38       result = connection.transaction(start_db_trans, &block)
39      
40       objects.each { |o| o.commit_transaction }
41       return result
42     ensure
43       connection.decrement_open_transactions
44       trap('TERM', previous_handler)
45     end
46   end
47 end