If the pool is not at a @max_connections limit, establish newconnection.
Connecting to the DB is done outside main synchronized section.
If a block is supplied, it is an additional constraint (checked while
holding the pool lock) on whether a newconnection
should be established.
# File activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb, line 1221
def try_to_checkout_new_connection
# first in synchronized section check if establishing new conns is allowed
# and increment @now_connecting, to prevent overstepping this pool's @max_connections
# constraint
do_checkout = synchronize do
return if self.discarded?
if @threads_blocking_new_connections.zero? && (@max_connections.nil? || (@connections.size + @now_connecting) < @max_connections) && (!block_given? || yield)
if @connections.size > 0 || @original_context != ActiveSupport::IsolatedExecutionState.context
@activated = true
end
@now_connecting += 1
end
end
if do_checkout
begin
# if successfully incremented @now_connecting establish new connection
# outside of synchronized section
conn = checkout_new_connection
ensure
synchronize do
@now_connecting -= 1
if conn
if self.discarded?
conn.discard!
else
adopt_connection(conn)
# returned conn needs to be already leased
conn.lease
end
end
end
end
end
end