了解建立连接在 ActiveRecord 中的工作原理 [英] Understanding how establish_connection works in ActiveRecord

查看:19
本文介绍了了解建立连接在 ActiveRecord 中的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码取自 ActiveRecord 2.3.14 的 gem 类 ConnectionHandler

This code was taken from ActiveRecord 2.3.14's gem class ConnectionHandler

def establish_connection(name, spec)
  @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)
end

似乎每次 ruby​​ 在模型上调用 establish_connection 时,它都会创建一个新的连接池.

It seems each time ruby calls establish_connection on the model, it's creating a new connection pool.

我的问题:

如果我有 5 个模型使用 establish_connection 到同一个数据库,Rails 是否足够聪明来选择一个已经存在的池而不是创建一个具有相同连接凭据的新池?如果我的 5 个模型是某个使用 establish_connection 的抽象类的子类,也会发生这种情况吗?如果存在,它是否总是从 @connection_pools 中选择一个连接?

If I have 5 models that use establish_connection to the same database, is Rails smart enough to pick an already existing pool rather creating a new one with the same connection credentials? Does this also happen if my 5 models are subclasses of some abstract class that uses establish_connection? Will it always pick a connection from the @connection_pools if it exists?

更新 1

我说的是一个具体的例子.您有 5 个具有 5 个不同连接的模型,每次 Rails 使用一个模型时,它都会执行 establish_connection.查看 ActiveRecord 中的代码,当它执行 establish_connection 时,它会创建一个新池,其中包含到该特定连接的连接.我想知道的是,每次 Rails 调用模型的 establish_connection 时,它是创建一个新池还是采用现有池.

I'm talking about a concrete example. You have 5 models with 5 different connections, each time Rails uses a model it executes establish_connection. Looking at the code in ActiveRecord, when it executes establish_connection it creates a new pool with connections to that specific connection. What I'm wondering is whether each time Rails calls a model's establish_connection, does it create a new pool or take the existing one.

例如:您来到我的网站并查看产品列表.您刚刚点击了一个调用 Product.all 的操作,该操作对 Amazon 上的某个数据库执行 establish_connection.然后,我来到产品列表,会发生什么?我是获取已建立的连接还是使用该连接创建新池?

Example: you come to my site and see a product list. You've just hit an action that calls Product.all, which executes establish_connection to some database on Amazon. Then, I come to the product list, what happens? Do I grab the established connection or am I creating a new pool with that connection?

更新 2

我的猜测是 Rails 第一次加载我的模型时,它会创建具有不同连接的池.之后,当我使用一些 Model.method 时,它只是获取与模型关联的连接并执行该方法.

My guess is that first time Rails loads my models it's creating pools with different connections. After, when I use some Model.method, it just grabs the connection associated with the model and executes the method.

我不确定当 2 个模型有两个相等的连接(不在抽象类中,而是在自类中)时会发生什么.这会产生两个相同的连接池,还是 ActiveRecord 足够聪明来捕捉这种情况?

I'm not sure what happens when 2 models have two equal connections (not in the abstract class but in self class). Will this produce two same connection pools, or is ActiveRecord smart enough to catch this case?

推荐答案

您确实不必在每个模型上调用 establish_connection.你可以简单地做下一步:

You really do not have to call establish_connection on each model. You can simply do next:

ActiveRecord::Base.establish_connection(
 { :adapter => 'mysql2',
   :database => 'some_database',
   :host => 'localhost',
   :username => 'root',
   :password => "" }
)

您将可以访问连接.(这块代码是从真实代码中提取的(数据库名称除外:))).
但是根据 API,我认为 Rails 不会从其他模型获取现有连接(如果我错了,请纠正我).
这里还有一个文档链接.您可以在那里阅读有关连接的更多信息.
希望对您有所帮助.

and you will have access to connection. (This chunk of code has been extracted from real code(except database name :) )).
But according to API I think that Rails does not take existing connection from other model (correct me if I am wrong).
Also here is a link to documentation. You can read more about the connection there.
I hope I helped you alittle.

这篇关于了解建立连接在 ActiveRecord 中的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆