在我们的Rails应用程序中,我们需要使用不同的数据库,具体取决于请求的子域(每个国家/地区的DB不同)。
现在我们正在做类似this question中建议的事情。也就是说,在每个请求上调用ActiveRecord::Base.establish_connection
。
但it seems ActiveRecord::Base.establish_connection
会删除当前连接池,并在每次调用时建立新连接。
我做了这个快速基准测试,看看每次调用establish_connection
和建立连接之间是否有任何显着差异:
require 'benchmark/ips'
$config = Rails.configuration.database_configuration[Rails.env]
$db1_config = $config.dup.update('database' => 'db1')
$db2_config = $config.dup.update('database' => 'db2')
# Method 1: call establish_connection on each "request".
Benchmark.ips do |r|
r.report('establish_connection:') do
# Simulate two requests, one for each DB.
ActiveRecord::Base.establish_connection($db1_config)
MyModel.count # A little query to force the DB connection to establish.
ActiveRecord::Base.establish_connection($db2_config)
MyModel.count
end
end
# Method 2: Have different subclasses of my models, one for each DB, and
# call establish_connection only once
class MyModelDb1 < MyModel
establish_connection($db1_config)
end
class MyModelDb2 < MyModel
establish_connection($db2_config)
end
Benchmark.ips do |r|
r.report('different models:') do
MyModelDb1.count
MyModelDb2.count
end
end
我用rails runner
运行这个脚本并指向一个本地mysql,在DB上有几千条记录,结果似乎表明事实上两者之间存在很大差异(一个数量级)这两种方法(BTW,我不确定基准是否有效或我搞砸了,因此结果有误导性):
Calculating -------------------------------------
establish_connection: 8 i/100ms
-------------------------------------------------
establish_connection: 117.9 (±26.3%) i/s - 544 in 5.001575s
Calculating -------------------------------------
different models: 119 i/100ms
-------------------------------------------------
different models: 1299.4 (±22.1%) i/s - 6188 in 5.039483s
所以,基本上,我想知道是否有办法维护每个子域的连接池,然后重新使用这些连接,而不是在每个请求上建立新的连接。为每个子域建立我的模型的子类是不可行的,因为有许多模型;我只想更改所有模型的连接(在ActiveRecord::Base
)
答案 0 :(得分:11)
好吧,我一直在深入研究这一点,并设法让事情发挥作用。
在阅读有关ActiveRecord中连接管理的tenderlove's post后,解释了类层次结构如何与连接管理不必要地结合在一起,我理解为什么要做我想做的事情并不像人们期望的那样直截了当。
我最终做的是继承ActiveRecord的ConnectionHandler并在我的模型层次结构的顶部使用新的连接处理程序(需要在ConnectionHandler code上摆弄一些,以了解它在内部如何工作;所以当然这个解决方案可能与我正在使用的Rails版本(3.2)非常相关。类似的东西:
# A model class that connects to a different DB depending on the subdomain
# we're in
class ModelBase < ActiveRecord::Base
self.abstract_class = true
self.connection_handler = CustomConnectionHandler.new
end
# ...
class CustomConnectionHandler < ActiveRecord::ConnectionAdapters::ConnectionHandler
def initialize
super
@pools_by_subdomain = {}
end
# Override the behaviour of ActiveRecord's ConnectionHandler to return a
# connection pool for the current domain.
def retrieve_connection_pool(klass)
# Get current subdomain somehow (Maybe store it in a class variable on
# each request or whatever)
subdomain = @@subdomain
@pools_by_subdomain[subdomain] ||= create_pool(subdomain)
end
private
def create_pool(subdomain)
conf = Rails.configuration.database_configuration[Rails.env].dup
# The name of the DB for that subdomain...
conf.update!('database' => "db_#{subdomain}")
resolver = ActiveRecord::Base::ConnectionSpecification::Resolver.new(conf, nil)
# Call ConnectionHandler#establish_connection, which receives a key
# (in this case the subdomain) for the new connection pool
establish_connection(subdomain, resolver.spec)
end
end
这仍然需要进行一些测试来检查实际上是否有性能提升,但我在本地Unicorn服务器上运行的初始测试表明存在。
答案 1 :(得分:0)
据我所知 Rails不会在请求之间维护它的数据库池,除非您使用多线程环境。喜欢Sidekiq。但是如果您在生产服务器上使用Passenger或Unicorn,它将为每个Rails实例创建一个新的数据库连接。
所以基本上使用数据库连接池是没用的,这意味着在每个请求上创建一个新的数据库连接不应该是一个问题。