instance method with_raw_connection

Ruby on Rails edge

Since v7.1.6 Private — implementation detail, not part of the public API

Available in: v7.1.6 v7.2.3.2 v8.0.5.1 v8.1.3.1 edge

Signature

with_raw_connection(allow_retry: false, materialize_transactions: true)

Lock the monitor, ensure we’re properly connected and transactions are materialized, and then yield the underlying raw connection object.

If allow_retry is true, a connection-related exception will cause an automatic reconnect and re-run of the block, up to the connection’s configured connection_retries setting and the configured retry_deadline limit. (Note that when allow_retry is true, it’s possible to return without having marked the connection as verified. If the block is guaranteed to exercise the connection, consider calling verified! to avoid needless verification queries in subsequent calls.)

If materialize_transactions is false, the block will be run without ensuring virtual transactions have been materialized in the DB server’s state. The active transaction will also remain clean (if it is not already dirty), meaning it’s able to be restored by reconnecting and opening an equivalent-depth set of new transactions. This should only be used by transaction control methods, and internal transaction-agnostic queries.

It’s not the primary use case, so not something to optimize for, but note that this method does need to be re-entrant: materialize_transactions will re-enter if it has work to do, and the yield block can also do so under some circumstances.

In the latter case, we really ought to guarantee the inner call will not reconnect (which would interfere with the still-yielded connection in the outer block), but we currently provide no special enforcement there.

Parameters

allow_retry key = false
materialize_transactions key = true
Source
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 1124
        def with_raw_connection(allow_retry: false, materialize_transactions: true)
          @lock.synchronize do
            reconnectable = ensure_connection_ready(allow_retry:, materialize_transactions:)

            budget = build_retry_budget(allow_retry:, reconnectable:)

            begin
              yield @raw_connection
            rescue => original_exception
              translated_exception = translate_exception_class(original_exception, nil, nil)
              invalidate_transaction(translated_exception)
              downgrade_connection_after_error(translated_exception)

              if attempt_retry(translated_exception, budget)
                if retryable_connection_error?(translated_exception)
                  ensure_connection_ready(allow_retry:, materialize_transactions: false)
                end
                retry
              end

              raise translated_exception
            rescue Exception
              # A non-StandardError (a Timeout, or a fiber scheduler's cancel) abandoned
              # the query partway through, so we mark the connection unverified, just as
              # a failed query would, forcing a reconnect before it's used again.
              @last_activity = nil
              @verified = false
              raise
            ensure
              dirty_current_transaction if materialize_transactions
            end
          end
        end

Defined in activerecord/lib/active_record/connection_adapters/abstract_adapter.rb line 1124 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ConnectionAdapters::AbstractAdapter

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close