了解如何establish_connection工程的ActiveRecord工程、establish_connection、ActiveRecord

2023-09-08 16:02:26 作者:nightmare(噩梦)

这code从ActiveRecord的2.3.14的宝石级采取 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 的典范。纵观code在ActiveRecord的,在执行时 establish_connection 它创建了连接到特定连接一个新池。我不知道是每次滑轨是否调用模型 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 其中执行 establish_connection 来在Amazon一些数据库的操作。于是,我来到了产品列表,会发生什么?难道我抢建立的连接还是我创建与该连接的新池?

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 => "" }
)

和你将有机会获得连接。 (中code此块摘自真正的code(不包括数据库名:)))。 但根据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.