instance method
with_connection
Ruby on Rails 8.0.4
Since v2.2.3Signature
with_connection(prevent_permanent_checkout: false)
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 #lease_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.
Parameters
-
prevent_permanent_checkoutkey = false
Source
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 409
def with_connection(prevent_permanent_checkout: false)
lease = connection_lease
sticky_was = lease.sticky
lease.sticky = false if prevent_permanent_checkout
if lease.connection
begin
yield lease.connection
ensure
lease.sticky = sticky_was if prevent_permanent_checkout && !sticky_was
end
else
begin
yield lease.connection = checkout
ensure
lease.sticky = sticky_was if prevent_permanent_checkout && !sticky_was
release_connection(lease) unless lease.sticky
end
end
end
Defined in activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb line 409
· View on GitHub
· Improve this page
· Find usages on GitHub