mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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`.
This commit is contained in:
parent
a59b9e2284
commit
0002954512
2 changed files with 26 additions and 4 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue