简体   繁体   English

如何使用Ruby on Rails创建新的ActiveRecord连接池?

[英]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.

相关问题 Rails:没有 ActiveRecord::Base 的连接池 - Rails: No connection pool for ActiveRecord::Base 如何使用ActiveRecord Rails 5打开和关闭新的数据库连接 - How do you open and close a new database connection with ActiveRecord Rails 5 如何将ActiveRecord Create用于Ruby on Rails的多个记录? - how can I use ActiveRecord Create for multiple records Ruby on Rails? 如何在Ruby on Rails中创建并保持串行端口连接,如何处理无限循环以使用新消息创建模型? - How to create and keep serialport connection in Ruby on Rails, handle infinity loop to create model with new messages? Rails4:如何获取ActiveRecord连接池中的空闲连接数? - Rails4: How can I get the number of idle connections in an ActiveRecord connection pool? Ruby / Rails数据库连接池实现 - Ruby/Rails db connection pool implementation 使用ActiveRecord连接动态创建Ruby类 - Dynamically create Ruby class with ActiveRecord connection 一旦达到连接池限制,带有MySQL的Rails ActiveRecord就会挂起 - Rails ActiveRecord w/ MySQL Hangs Once Connection Pool Limit is Reached 如何在非 Rails 应用程序中使用 ActiveRecord 创建一个新的 MySQL 数据库? - How to create a new MySQL database with ActiveRecord in a non-rails app? Ruby on Rails:从 activerecord 查询创建数组 - Ruby on Rails : create array from activerecord query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM