method
within_new_transaction
v7.0.0 -
Show latest stable
- Class:
ActiveRecord::ConnectionAdapters::TransactionManager
within_new_transaction(isolation: nil, joinable: true)public
No documentation available.
# File activerecord/lib/active_record/connection_adapters/abstract/transaction.rb, line 316
def within_new_transaction(isolation: nil, joinable: true)
@connection.lock.synchronize do
transaction = begin_transaction(isolation: isolation, joinable: joinable)
ret = yield
completed = true
ret
rescue Exception => error
if transaction
transaction.state.invalidate! if error.is_a? ActiveRecord::TransactionRollbackError
rollback_transaction
after_failure_actions(transaction, error)
end
raise
ensure
if transaction
if error
# @connection still holds an open or invalid transaction, so we must not
# put it back in the pool for reuse.
@connection.throw_away! unless transaction.state.rolledback?
elsif Thread.current.status == "aborting" || (!completed && transaction.written)
# The transaction is still open but the block returned earlier.
#
# The block could return early because of a timeout or because the thread is aborting,
# so we are rolling back to make sure the timeout didn't caused the transaction to be
# committed incompletely.
rollback_transaction
else
begin
commit_transaction
rescue Exception
rollback_transaction(transaction) unless transaction.state.completed?
raise
end
end
end
end
end