aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-12-30 14:56:26 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-12-30 14:56:26 -0800
commitcceabe03a80c4a0121a2b1187b56a2d29586a43b (patch)
tree72d628482b85655a9d2898fbe2ba3648174a0d49 /activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
parentb1ac881433b95498e3b63b2686d1e31f1bdb93fc (diff)
downloadrails-cceabe03a80c4a0121a2b1187b56a2d29586a43b.tar.gz
rails-cceabe03a80c4a0121a2b1187b56a2d29586a43b.tar.bz2
rails-cceabe03a80c4a0121a2b1187b56a2d29586a43b.zip
raise a pull full error when the connection pool is full and no connection can be obtained
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb38
1 files changed, 20 insertions, 18 deletions
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 4173b3ebe6..124875d52f 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -9,6 +9,13 @@ module ActiveRecord
class ConnectionTimeoutError < ConnectionNotEstablished
end
+ # Raised when a connection pool is full and another connection is requested
+ class PoolFullError < ConnectionNotEstablished
+ def initialize size, timeout
+ super("Connection pool of size #{size} and timeout #{timeout}s is full")
+ end
+ end
+
module ConnectionAdapters
# Connection pool base class for managing Active Record database
# connections.
@@ -60,7 +67,7 @@ module ActiveRecord
include MonitorMixin
attr_accessor :automatic_reconnect, :timeout
- attr_reader :spec, :connections
+ attr_reader :spec, :connections, :size
# Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
# object which describes database connection information (e.g. adapter,
@@ -160,37 +167,33 @@ module ActiveRecord
end
end
- # Return any checked-out connections back to the pool by threads that
- # are no longer alive.
- def clear_stale_cached_connections!
+ def clear_stale_cached_connections! # :nodoc:
end
deprecate :clear_stale_cached_connections!
# Check-out a database connection from the pool, indicating that you want
# to use it. You should call #checkin when you no longer need this.
#
- # This is done by either returning an existing connection, or by creating
- # a new connection. If the maximum number of connections for this pool has
- # already been reached, but the pool is empty (i.e. they're all being used),
- # then this method will wait until a thread has checked in a connection.
- # The wait time is bounded however: if no connection can be checked out
- # within the timeout specified for this pool, then a ConnectionTimeoutError
- # exception will be raised.
+ # This is done by either returning and leasing existing connection, or by
+ # creating a new connection and leasing it.
+ #
+ # If all connections are leased and the pool is at capacity (meaning the
+ # number of currently leased connections is greater than or equal to the
+ # size limit set), an ActiveRecord::PoolFullError exception will be raised.
#
# Returns: an AbstractAdapter object.
#
# Raises:
- # - ConnectionTimeoutError: no connection can be obtained from the pool
- # within the timeout period.
+ # - PoolFullError: no connection can be obtained from the pool.
def checkout
# Checkout an available connection
synchronize do
- # Try to find a connection that hasn't been leased
- conn = @connections.find { |c| c.lease }
+ # Try to find a connection that hasn't been leased, and lease it
+ conn = connections.find { |c| c.lease }
# If all connections were leased, and we have room to expand,
# create a new connection and lease it.
- if !conn && @connections.size < @size
+ if !conn && connections.size < size
conn = checkout_new_connection
conn.lease
end
@@ -198,9 +201,8 @@ module ActiveRecord
if conn
checkout_and_verify conn
else
- raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
+ raise PoolFullError.new(size, timeout)
end
-
end
end