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-11-29 15:30:04 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-11-29 15:30:04 -0800
commitc606fe2c6f1112d13b5ff01f3204e29cbdb09649 (patch)
tree477d38aefc5927953e04e30eaaef5b72e4b1eaf2 /activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
parent0e2477b602b3aa5b66c849d19737a8b66c73f633 (diff)
downloadrails-c606fe2c6f1112d13b5ff01f3204e29cbdb09649.tar.gz
rails-c606fe2c6f1112d13b5ff01f3204e29cbdb09649.tar.bz2
rails-c606fe2c6f1112d13b5ff01f3204e29cbdb09649.zip
push synchronization in to each method. Reduces method calls and makes
it clear which methods are synchronized.
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.rb54
1 files changed, 29 insertions, 25 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 d127b3ebd8..698da34d26 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -1,7 +1,6 @@
require 'thread'
require 'monitor'
require 'set'
-require 'active_support/core_ext/module/synchronization'
require 'active_support/core_ext/module/deprecation'
module ActiveRecord
@@ -58,6 +57,8 @@ module ActiveRecord
# * +wait_timeout+: number of seconds to block and wait for a connection
# before giving up and raising a timeout error (default 5 seconds).
class ConnectionPool
+ include MonitorMixin
+
attr_accessor :automatic_reconnect
attr_reader :spec, :connections
@@ -68,14 +69,14 @@ module ActiveRecord
#
# The default ConnectionPool maximum size is 5.
def initialize(spec)
+ super()
+
@spec = spec
# The cache of reserved connections mapped to threads
@reserved_connections = {}
- # The mutex used to synchronize pool access
- @connection_mutex = Monitor.new
- @queue = @connection_mutex.new_cond
+ @queue = new_cond
@timeout = spec.config[:wait_timeout] || 5
# default max pool size to 5
@@ -121,37 +122,43 @@ module ActiveRecord
# Returns true if a connection has already been opened.
def connected?
- @connections.any?
+ synchronize { @connections.any? }
end
# Disconnects all connections in the pool, and clears the pool.
def disconnect!
- @reserved_connections = {}
- @connections.each do |conn|
- checkin conn
- conn.disconnect!
+ synchronize do
+ @reserved_connections = {}
+ @connections.each do |conn|
+ checkin conn
+ conn.disconnect!
+ end
+ @connections = []
end
- @connections = []
end
# Clears the cache which maps classes.
def clear_reloadable_connections!
- @reserved_connections = {}
- @connections.each do |conn|
- checkin conn
- conn.disconnect! if conn.requires_reloading?
- end
- @connections.delete_if do |conn|
- conn.requires_reloading?
+ synchronize do
+ @reserved_connections = {}
+ @connections.each do |conn|
+ checkin conn
+ conn.disconnect! if conn.requires_reloading?
+ end
+ @connections.delete_if do |conn|
+ conn.requires_reloading?
+ end
end
end
# Verify active connections and remove and disconnect connections
# associated with stale threads.
def verify_active_connections! #:nodoc:
- clear_stale_cached_connections!
- @connections.each do |connection|
- connection.verify!
+ synchronize do
+ clear_stale_cached_connections!
+ @connections.each do |connection|
+ connection.verify!
+ end
end
end
@@ -219,7 +226,7 @@ connection. For example: ActiveRecord::Base.connection.close
# within the timeout period.
def checkout
# Checkout an available connection
- @connection_mutex.synchronize do
+ synchronize do
loop do
conn = @connections.find { |c| c.lease }
@@ -256,7 +263,7 @@ connection. For example: ActiveRecord::Base.connection.close
# +conn+: an AbstractAdapter object, which was obtained by earlier by
# calling +checkout+ on this pool.
def checkin(conn)
- @connection_mutex.synchronize do
+ synchronize do
conn.run_callbacks :checkin do
conn.expire
@queue.signal
@@ -264,9 +271,6 @@ connection. For example: ActiveRecord::Base.connection.close
end
end
- synchronize :clear_reloadable_connections!, :verify_active_connections!,
- :connected?, :disconnect!, :with => :@connection_mutex
-
private
def new_connection