简体   繁体   English

活动记录重置数据库连接问题 - ActiveRecord :: Connection.clear_active_connections

[英]Active record reset database connection issues - ActiveRecord::Connection.clear_active_connections

Can some body explain me what does these two function exactly do, i am not able to understand it through rails api. 有些机构可以解释一下这两个函数到底做了什么,我无法通过rails api理解它。

  ActiveRecord::Connection.clear_active_connections!
  ActiveRecord::Connection.clear_all_connections!()

I am working on an application, in which i have to make dynamic db connection. 我正在开发一个应用程序,我必须在其中建立动态数据库连接。 Do i need to put these lines before making a new connection. 我是否需要在建立新连接之前放置这些行。 When i make a new connection what happens to old connection, since i do not remove it explicitly, does this happens automatically ? 当我建立一个新连接时,旧连接会发生什么,因为我没有明确删除它,这是否会自动发生?

First off, I think you want ActiveRecord::Base.connection 首先,我想你想要ActiveRecord::Base.connection

I'm looking at these issues now. 我现在正在看这些问题。 As best I can tell ActiveRecord::Base.establish_connection will drop all prior connections from the pool and only use the new connection. 我可以告诉ActiveRecord::Base.establish_connection将从池中删除所有先前的连接并仅使用新连接。

I'm running into issues when I'm swapping back and forth and changing connections both at the ActiveRecord::Base level and at the individual model level (ie User.establish_connection . Under certain circumstances, I find that if I establish a connection on the model, then establish a connection on ActiveRecord::Base (intending to also use the new connection on the model), the model will retain the connection. 当我在ActiveRecord::Base级别和单个模型级别(即User.establish_connection来回交换时,我User.establish_connection 。在某些情况下,我发现如果我建立连接该模型,然后在ActiveRecord::Base上建立连接(打算也使用模型上的新连接),模型将保留连接。

To wit: 以机智:

$ ActiveRecord::Base.connection_config[:host]
-> main
$ User.connection_config[:host]
-> main

$ User.establish_connection :blah
$ User.connection_config[:host]
-> blah
$ ActiveRecord::Base.connection_config[:host]
-> main

$ ActiveRecord::Base.establish_connection :blah
$ User.connection_config[:host]
-> blah
$ ActiveRecord::Base.connection_config[:host]
-> blah

$ ActiveRecord::Base.establish_connection :main
$ User.connection_config[:host]
-> blah
$ ActiveRecord::Base.connection_config[:host]
-> main

The important point to note here is that, since you've established the connection on the User model explicitly, ActiveRecord knows to use a different connection pool than that used for the other children of ActiveRecord::Base ; 这里要注意的重点是,由于您已明确地在User模型上建立了连接,因此ActiveRecord知道使用与ActiveRecord::Base的其他子ActiveRecord::Base不同的连接池; thus, when you switch the ActiveRecord::Base connection back, it doesn't switch all models, just the ones sharing the main connection pool. 因此,当您切换回ActiveRecord::Base连接时,它不会切换所有模型,只会切换共享主连接池的模型。

Here's the ConnectionHandler doc: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html 这是ConnectionHandler文档: http//api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM