instance method
acquire_connection
Ruby on Rails 7.2.3
Since v4.0.13 PrivateSignature
acquire_connection(checkout_timeout)
Acquire a connection by one of 1) immediately removing one from the queue of available connections, 2) creating a new connection if the pool is not at capacity, 3) waiting on the queue for a connection to become available.
Raises:
-
ActiveRecord::ConnectionTimeoutError if a connection could not be acquired
Parameters
-
checkout_timeoutreq
Source
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 875
def acquire_connection(checkout_timeout)
# NOTE: we rely on <tt>@available.poll</tt> and +try_to_checkout_new_connection+ to
# +conn.lease+ the returned connection (and to do this in a +synchronized+
# section). This is not the cleanest implementation, as ideally we would
# <tt>synchronize { conn.lease }</tt> in this method, but by leaving it to <tt>@available.poll</tt>
# and +try_to_checkout_new_connection+ we can piggyback on +synchronize+ sections
# of the said methods and avoid an additional +synchronize+ overhead.
if conn = @available.poll || try_to_checkout_new_connection
conn
else
reap
# Retry after reaping, which may return an available connection,
# remove an inactive connection, or both
if conn = @available.poll || try_to_checkout_new_connection
conn
else
@available.poll(checkout_timeout)
end
end
rescue ConnectionTimeoutError => ex
raise ex.set_pool(self)
end
Defined in activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb line 875
· View on GitHub
· Improve this page
· Find usages on GitHub