diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 233e27b21178f..74d30891aca17 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -541,12 +541,14 @@ def clear_reloadable_connections! # - ActiveRecord::ConnectionTimeoutError no connection can be obtained from the pool. def checkout(checkout_timeout = @checkout_timeout) if @pinned_connection - synchronize do - @pinned_connection.verify! - # Any leased connection must be in @connections otherwise - # some methods like #connected? won't behave correctly - unless @connections.include?(@pinned_connection) - @connections << @pinned_connection + @pinned_connection.lock.synchronize do + synchronize do + @pinned_connection.verify! + # Any leased connection must be in @connections otherwise + # some methods like #connected? won't behave correctly + unless @connections.include?(@pinned_connection) + @connections << @pinned_connection + end end end @pinned_connection