Skip to content

Commit 53f4a4b

Browse files
authored
Add invalidate! directly to Transaction
Every instance of `Transaction` including instances of `NullTransaction` now respond to `invalidate!` which allows us to use transactions in a "tell do not ask" way. This should simplify transaction invalidation for cases were we needed check whether transaction is not a `NullTransaction` to prevent calling `invalidate!` on `nil` state.
1 parent 9253d40 commit 53f4a4b

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

activerecord/lib/active_record/connection_adapters/abstract/transaction.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,15 @@ def add_record(record, _ = true); end
8383
def restartable?; false; end
8484
def dirty?; false; end
8585
def dirty!; end
86+
def invalidate!; end
8687
end
8788

8889
class Transaction # :nodoc:
8990
attr_reader :connection, :state, :savepoint_name, :isolation_level
9091
attr_accessor :written, :written_indirectly
9192

93+
delegate :invalidate!, to: :@state
94+
9295
def initialize(connection, isolation: nil, joinable: true, run_commit_callbacks: false)
9396
@connection = connection
9497
@state = TransactionState.new
@@ -496,7 +499,7 @@ def within_new_transaction(isolation: nil, joinable: true)
496499
begin
497500
commit_transaction
498501
rescue ActiveRecord::ConnectionFailed
499-
transaction.state.invalidate! unless transaction.state.completed?
502+
transaction.invalidate! unless transaction.state.completed?
500503
raise
501504
rescue Exception
502505
rollback_transaction(transaction) unless transaction.state.completed?

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ def invalidate_transaction(exception)
10121012
return unless exception.is_a?(TransactionRollbackError)
10131013
return unless savepoint_errors_invalidate_transactions?
10141014

1015-
current_transaction.state.invalidate! if current_transaction.open?
1015+
current_transaction.invalidate!
10161016
end
10171017

10181018
def retryable_query_error?(exception)

0 commit comments

Comments
 (0)