Ensure a lost connection upon COMMIT fails "cleanly"

There's no point attempting to rollback in this case; we can instead
just invalidate the now-lost transaction.

Similarly, even though we can't immediately reconnect for a connection
failure mid-transaction, we can and should drop and prior verification
of the connection: the next out-of-transaction query attempt needs to
fix it, not assume it's working.
This commit is contained in:
Matthew Draper 2022-06-08 21:59:36 +09:30
parent 8551e64e24
commit 0e9267767f
2 changed files with 9 additions and 0 deletions

@ -495,6 +495,9 @@ def within_new_transaction(isolation: nil, joinable: true)
begin
commit_transaction
rescue ActiveRecord::ConnectionFailed
transaction.state.invalidate! unless transaction.state.completed?
raise
rescue Exception
rollback_transaction(transaction) unless transaction.state.completed?
raise

@ -941,6 +941,12 @@ def with_raw_connection(allow_retry: false, uses_transaction: true)
end
end
if retryable_connection_error?(translated_exception)
# There's a problem with the connection, but we couldn't attempt to
# reconnect. The connection is going to stay broken, so un-verify it.
@verified = false
end
raise translated_exception
ensure
dirty_current_transaction if uses_transaction