diff options
author | Arthur Neves <arthurnn@gmail.com> | 2014-08-08 15:37:38 -0400 |
---|---|---|
committer | Arthur Neves <arthurnn@gmail.com> | 2014-08-15 16:04:02 -0400 |
commit | 0002954512364f2f69d28798f7a79aa8e27d7b6b (patch) | |
tree | 1d50aa0c2a23d3964ae2ee29d03939c855614955 | |
parent | a59b9e2284353dcf8cf6d77b97aaa4255563c807 (diff) | |
download | rails-0002954512364f2f69d28798f7a79aa8e27d7b6b.tar.gz rails-0002954512364f2f69d28798f7a79aa8e27d7b6b.tar.bz2 rails-0002954512364f2f69d28798f7a79aa8e27d7b6b.zip |
Use *_transaction methods in TransactionManager
Use `commit_transaction`/`rollback_transaction` on
`within_new_transaction` method, so they make sure they `pop` the
transaction from the stack before calling the methods `commit`/`rollback`.
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/transaction.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/transactions_test.rb | 24 |
2 files changed, 26 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb index 4a7f2aaca8..3fce32211d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb @@ -169,16 +169,14 @@ module ActiveRecord transaction = begin_transaction options yield rescue Exception => error - transaction.rollback if transaction + rollback_transaction if transaction raise ensure begin - transaction.commit unless error + commit_transaction unless error rescue Exception transaction.rollback raise - ensure - @stack.pop if transaction end end diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index b4849222b8..9cfe041de2 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -80,6 +80,30 @@ class TransactionTest < ActiveRecord::TestCase end end + def test_number_of_transactions_in_commit + num = nil + + Topic.connection.class_eval do + alias :real_commit_db_transaction :commit_db_transaction + define_method(:commit_db_transaction) do + num = transaction_manager.open_transactions + real_commit_db_transaction + end + end + + Topic.transaction do + @first.approved = true + @first.save! + end + + assert_equal 0, num + ensure + Topic.connection.class_eval do + remove_method :commit_db_transaction + alias :commit_db_transaction :real_commit_db_transaction rescue nil + end + end + def test_successful_with_instance_method @first.transaction do @first.approved = true |