Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix race in ConnectionPool#checkout
After releasing monitor some connection(s) may appear in pool before monitor is re-aquired.
When this happens we'll wait for connection which is already available.
  • Loading branch information
Aliaksey Kandratsenka committed Oct 4, 2008
1 parent db5d9ae commit e712798
Showing 1 changed file with 14 additions and 15 deletions.
Expand Up @@ -131,21 +131,20 @@ def clear_stale_cached_connections!
# Check-out a database connection from the pool.
def checkout
# Checkout an available connection
conn = @connection_mutex.synchronize do
if @checked_out.size < @connections.size
checkout_existing_connection
elsif @connections.size < @size
checkout_new_connection
end
end
return conn if conn

# No connections available; wait for one
@connection_mutex.synchronize do
if @queue.wait(@timeout)
checkout_existing_connection
else
raise ConnectionTimeoutError, "could not obtain a database connection within #{@timeout} seconds. The pool size is currently #{@size}, perhaps you need to increase it?"
loop do
conn = if @checked_out.size < @connections.size
checkout_existing_connection
elsif @connections.size < @size
checkout_new_connection
end
return conn if conn
# No connections available; wait for one
if @queue.wait(@timeout)
next
else
raise ConnectionTimeoutError, "could not obtain a database connection within #{@timeout} seconds. The pool size is currently #{@size}, perhaps you need to increase it?"
end
end
end
end
Expand Down Expand Up @@ -275,4 +274,4 @@ def retrieve_connection_pool(klass)
end
end
end
end
end

0 comments on commit e712798

Please sign in to comment.