Skip to content
Browse files

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`.
  • Loading branch information...
1 parent a59b9e2 commit 0002954512364f2f69d28798f7a79aa8e27d7b6b @arthurnn arthurnn committed Aug 8, 2014
View
6 activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
@@ -169,16 +169,14 @@ def within_new_transaction(options = {})
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
View
24 activerecord/test/cases/transactions_test.rb
@@ -80,6 +80,30 @@ def test_successful_with_return
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

0 comments on commit 0002954

Please sign in to comment.
Something went wrong with that request. Please try again.