[英]How to create a new ActiveRecord Connection Pool with Ruby on Rails?
I have a RoR app that works great with the main RoR database. 我有一个RoR应用程序,适用于主要的RoR数据库。 I also want to connect to arbitrary databases in a way such that I won't recreate the database connection with each new HTTP request.
我还想以一种方式连接到任意数据库,这样我就不会为每个新的HTTP请求重新创建数据库连接。 From my research it looks like a connection pool is the way to go.
根据我的研究,它看起来像连接池是要走的路。
However, I am having trouble figuring out how to create the pool itself: 但是,我无法弄清楚如何创建池本身:
config = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new( {
"adapter" => "postgresql",
"host" => "localhost",
"port" => 9000,
"database" => "foo_test",
"username" => "foo",
"password" => "bar",
"pool" => "5",
"timeout" => "3000"
})
my_connection_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(config)
This errors with NameError: uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionSpecification. NameError的错误:未初始化的常量ActiveRecord :: ConnectionAdapters :: ConnectionSpecification。 Interstingly ActiveRecord::ConnectionAdapters::ConnectionPool works fine (but doesn't have the right config object).
Interstingly ActiveRecord :: ConnectionAdapters :: ConnectionPool工作正常(但没有正确的配置对象)。
What do I do here? 我该怎么办?
And also, are these the right things to be using to solve my problem? 而且,这些是用来解决我的问题的正确方法吗?
Depending on your use case, Rails might do this for you automatically. 根据您的使用情况,Rails可能会自动为您执行此操作。 Each
ActiveRecord::Base
subclass looks up the inheritance chain to find its pool, so 每个
ActiveRecord::Base
子类都会查找继承链以查找其池,因此
class Foo < ActiveRecord::Base
end
Uses the ActiveRecord::Base
connection pool, but 使用
ActiveRecord::Base
连接池,但是
class Bar < ActiveRecord::Base
establish_connection database: 'some_other_db'
end
class BarSub < Bar
end
would both connect to 'some_other_db' using the Bar
pool. 将使用
Bar
池连接到'some_other_db'。
I don't remember exactly, but I think every time you use ActiveRecord::Base.establish_connection
(or establish_connection
on any subclass) it creates a new pool. 我不记得确切,但我想每次使用
ActiveRecord::Base.establish_connection
(或任何子类的establish_connection
)时,它都会创建一个新池。 Doing otherwise would be absurd! 否则将是荒谬的!
A possibly helpful SO post on establish_connection 关于establish_connection的一个可能有用的SO帖子
I just have a separate file in my initializers that i use to create multiple connection with. 我只是在我的初始化程序中有一个单独的文件,用于创建多个连接。 The lib file is :
lib文件是:
require 'active_record'
module whateverName
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
def base(dbName)
establish_connection(ActiveRecord::Base.configurations[dbName.to_s][Rails.env])
end
end
end
Keep that file in lib and in models just include this and pass the database type. 将该文件保存在lib中,并在模型中包含此文件并传递数据库类型。 Make sure database.yml has the required configurations.
确保database.yml具有所需的配置。 Model will be something like :
模型将是这样的:
class Bar < ActiveRecord::Base
include whateverName
base :oracle
end
All you need to have is a config for oracle in database.yml. 您需要拥有的只是database.yml中的oracle配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.