mirror of
https://github.com/github/rails.git
synced 2026-04-04 03:00:58 -04:00
Remove CachedConnectionPerThread per-thread pooling mechanism in favor of a fixed pool with default maximum of 5 connections
This commit is contained in:
@@ -36,12 +36,10 @@ module ActiveRecord
|
||||
# * +wait_timeout+ (optional): number of seconds to block and wait
|
||||
# for a connection before giving up and raising a timeout error.
|
||||
def self.create(spec)
|
||||
if spec.config[:pool] && spec.config[:pool].to_i > 0
|
||||
FixedSizeConnectionPool.new(spec)
|
||||
elsif spec.config[:jndi] # JRuby appserver datasource pool; passthrough
|
||||
if spec.config[:jndi] # JRuby appserver datasource pool; passthrough
|
||||
NewConnectionEveryTime.new(spec)
|
||||
else
|
||||
CachedConnectionPerThread.new(spec)
|
||||
FixedSizeConnectionPool.new(spec)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -204,18 +202,6 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
|
||||
# CachedConnectionPerThread is a compatible pseudo-connection pool that
|
||||
# caches connections per-thread. In order to hold onto threads in the same
|
||||
# manner as ActiveRecord 2.1 and earlier, it only disconnects the
|
||||
# connection when the connection is checked in, or when calling
|
||||
# ActiveRecord::Base.clear_all_connections!, and not during
|
||||
# #release_connection.
|
||||
class CachedConnectionPerThread < NewConnectionEveryTime
|
||||
def release_connection
|
||||
# no-op; keep the connection
|
||||
end
|
||||
end
|
||||
|
||||
# FixedSizeConnectionPool provides a full, fixed-size connection pool with
|
||||
# timed waits when the pool is exhausted.
|
||||
class FixedSizeConnectionPool < ConnectionPool
|
||||
@@ -223,7 +209,8 @@ module ActiveRecord
|
||||
super
|
||||
# default 5 second timeout
|
||||
@timeout = spec.config[:wait_timeout] || 5
|
||||
@size = spec.config[:pool].to_i
|
||||
# default max pool size to 5
|
||||
@size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
|
||||
@queue = @connection_mutex.new_cond
|
||||
@connections = []
|
||||
@checked_out = []
|
||||
|
||||
@@ -285,6 +285,7 @@ module ActiveRecord
|
||||
# See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
|
||||
# reset the connection is to change the user to the same user.
|
||||
@connection.change_user(@config[:username], @config[:password], @config[:database])
|
||||
configure_connection
|
||||
else
|
||||
super
|
||||
end
|
||||
@@ -538,7 +539,11 @@ module ActiveRecord
|
||||
end
|
||||
|
||||
@connection.real_connect(*@connection_options)
|
||||
configure_connection
|
||||
end
|
||||
|
||||
def configure_connection
|
||||
encoding = @config[:encoding]
|
||||
execute("SET NAMES '#{encoding}'") if encoding
|
||||
|
||||
# By default, MySQL 'where id is null' selects the last inserted id.
|
||||
|
||||
@@ -955,7 +955,7 @@ module Test #:nodoc:
|
||||
ActiveRecord::Base.connection.rollback_db_transaction
|
||||
ActiveRecord::Base.connection.decrement_open_transactions
|
||||
end
|
||||
ActiveRecord::Base.verify_active_connections!
|
||||
ActiveRecord::Base.clear_active_connections!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -280,6 +280,7 @@ unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :OpenBaseAdapter)
|
||||
sleep zzz # block thread 2 for zzz seconds
|
||||
end
|
||||
t1 = Time.now
|
||||
Person.clear_active_connections!
|
||||
end
|
||||
|
||||
b = Thread.new do
|
||||
@@ -287,6 +288,7 @@ unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :OpenBaseAdapter)
|
||||
t2 = Time.now
|
||||
Person.transaction { yield }
|
||||
t3 = Time.now
|
||||
Person.clear_active_connections!
|
||||
end
|
||||
|
||||
a.join
|
||||
|
||||
@@ -16,12 +16,14 @@ unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name
|
||||
|
||||
class PooledConnectionsTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
super
|
||||
@connection = ActiveRecord::Base.remove_connection
|
||||
end
|
||||
|
||||
def teardown
|
||||
ActiveRecord::Base.clear_all_connections!
|
||||
ActiveRecord::Base.establish_connection(@connection)
|
||||
super
|
||||
end
|
||||
|
||||
def checkout_connections
|
||||
|
||||
@@ -296,6 +296,7 @@ if current_adapter?(:PostgreSQLAdapter)
|
||||
topic.approved = !topic.approved?
|
||||
topic.save!
|
||||
end
|
||||
Topic.clear_active_connections!
|
||||
end
|
||||
end
|
||||
|
||||
@@ -331,6 +332,7 @@ if current_adapter?(:PostgreSQLAdapter)
|
||||
dev = Developer.find(1)
|
||||
assert_equal original_salary, dev.salary
|
||||
end
|
||||
Developer.clear_active_connections!
|
||||
end
|
||||
end
|
||||
|
||||
@@ -342,6 +344,7 @@ if current_adapter?(:PostgreSQLAdapter)
|
||||
# Always expect original salary.
|
||||
assert_equal original_salary, Developer.find(1).salary
|
||||
end
|
||||
Developer.clear_active_connections!
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user