Permalink
Browse files

Use connection lease to determine "checked_out" connections

  • Loading branch information...
1 parent f866f66 commit b72b477c373b54200bfc49c8c0b0f9e42e7e68e3 @tenderlove tenderlove committed Nov 29, 2011
Showing with 21 additions and 17 deletions.
  1. +21 −17 activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -82,7 +82,6 @@ def initialize(spec)
@size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
@connections = []
- @checked_out = []
@automatic_reconnect = true
end
@@ -216,20 +215,27 @@ def checkout
# Checkout an available connection
@connection_mutex.synchronize do
loop do
- conn = if @checked_out.size < @connections.size
- checkout_existing_connection
- elsif @connections.size < @size
- checkout_new_connection
- end
- return conn if conn
+ conn = @connections.find { |c| c.lease }
+
+ unless conn
+ if @connections.size < @size
+ conn = checkout_new_connection
+ conn.lease
+ end
+ end
+
+ if conn
+ checkout_and_verify conn
+ return conn
+ end
@queue.wait(@timeout)
- if(@checked_out.size < @connections.size)
+ if(checked_out.size < @connections.size)
next
else
clear_stale_cached_connections!
- if @size == @checked_out.size
+ if @size == checked_out.size
raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
end
end
@@ -246,7 +252,7 @@ def checkout
def checkin(conn)
@connection_mutex.synchronize do
conn.run_callbacks :checkin do
- @checked_out.delete conn
+ conn.expire
@queue.signal
end
end
@@ -270,21 +276,19 @@ def checkout_new_connection
c = new_connection
@connections << c
- checkout_and_verify(c)
- end
-
- def checkout_existing_connection
- c = (@connections - @checked_out).first
- checkout_and_verify(c)
+ c
end
def checkout_and_verify(c)
c.run_callbacks :checkout do
c.verify!
- @checked_out << c
end
c
end
+
+ def checked_out
+ @connections.find_all { |c| c.in_use? }
+ end
end
# ConnectionHandler is a collection of ConnectionPool objects. It is used

0 comments on commit b72b477

Please sign in to comment.