diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-09-14 20:55:01 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-09-15 00:00:50 +0100 |
commit | 748052a99bb3046e4cdb7f8c00457da74fbdb75b (patch) | |
tree | ea818f8986f2f8513b0b0619063b8561647041d2 | |
parent | 280587588aba6ce13717cd6679e3f2b43d287443 (diff) | |
download | rails-748052a99bb3046e4cdb7f8c00457da74fbdb75b.tar.gz rails-748052a99bb3046e4cdb7f8c00457da74fbdb75b.tar.bz2 rails-748052a99bb3046e4cdb7f8c00457da74fbdb75b.zip |
Remove the transaction_open variable
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb | 49 | ||||
-rw-r--r-- | activerecord/test/cases/transactions_test.rb | 5 |
2 files changed, 33 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index dd379446e9..0589e6b447 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -3,7 +3,7 @@ module ActiveRecord module DatabaseStatements def initialize super - @transaction = ClosedTransaction.new(self) + reset_transaction end # Converts an arel AST to SQL @@ -172,37 +172,40 @@ module ActiveRecord def transaction(options = {}) options.assert_valid_keys :requires_new, :joinable - transaction_open = false - - begin - if options[:requires_new] || !current_transaction.joinable? - begin_transaction(options) - transaction_open = true - end - - yield - rescue Exception => error - if !outside_transaction? && transaction_open - rollback_transaction - transaction_open = false - end - - raise unless error.is_a?(ActiveRecord::Rollback) + if !options[:requires_new] && current_transaction.joinable? + within_existing_transaction { yield } + else + within_new_transaction(options) { yield } end + rescue Exception => error + raise unless error.is_a?(ActiveRecord::Rollback) + end + def within_new_transaction(options = {}) #:nodoc: + begin_transaction(options) + yield + rescue Exception => error + rollback_transaction unless outside_transaction? + raise ensure if outside_transaction? - @transaction = ClosedTransaction.new(self) - elsif current_transaction.open? && transaction_open + reset_transaction + else begin - commit_transaction - rescue Exception + commit_transaction unless error + rescue Exception => e rollback_transaction raise end end end + def within_existing_transaction #:nodoc: + yield + ensure + reset_transaction if outside_transaction? + end + def current_transaction #:nodoc: @transaction end @@ -225,6 +228,10 @@ module ActiveRecord @transaction = @transaction.rollback end + def reset_transaction + @transaction = ClosedTransaction.new(self) + end + # Register a record with the current transaction so that its after_commit and after_rollback callbacks # can be called. def add_transaction_record(record) diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 9ab2bddeb2..e618c0984e 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -423,6 +423,11 @@ class TransactionTest < ActiveRecord::TestCase assert_equal 0, Topic.connection.open_transactions end assert_equal 0, Topic.connection.open_transactions + + Topic.transaction do + Topic.connection.rollback_db_transaction + end + assert_equal 0, Topic.connection.open_transactions end end |