mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove the transaction_open variable
This commit is contained in:
parent
280587588a
commit
748052a99b
2 changed files with 33 additions and 21 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue