instance method
with_connection
Ruby on Rails 7.1.6
Since v2.2.3Signature
with_connection()
Yields a connection from the connection pool to the block. If no connection is already checked out by the current thread, a connection will be checked out from the pool, yielded to the block, and then returned to the pool when the block is finished. If a connection has already been checked out on the current thread, such as via #connection or #with_connection, that existing connection will be the one yielded and it will not be returned to the pool automatically at the end of the block; it is expected that such an existing connection will be properly returned to the pool by the code that checked it out.
Source
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 222
def with_connection
unless conn = @thread_cached_conns[connection_cache_key(ActiveSupport::IsolatedExecutionState.context)]
conn = connection
fresh_connection = true
end
yield conn
ensure
release_connection if fresh_connection
end
Defined in activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb line 222
· View on GitHub
· Improve this page
· Find usages on GitHub