[英]What is the impact of ActiveRecord::Base.establish_connection in Rails?
I use Rails in production with Passenger. 我在旅客中使用Rails。 The app is able to handle multiple requests in parallel
该应用程序能够并行处理多个请求
In a migration helper code I establish a connection to an old database to copy data to the new one. 在迁移助手代码中,我建立了到旧数据库的连接,以将数据复制到新数据库。
As the connection is made by calling ActiveRecord::Base.establish_connection
, does it affect only the current request or may it have some side effects ? 由于通过调用
ActiveRecord::Base.establish_connection
,因此它仅影响当前请求,还是可能会有一些副作用?
Just after getting the data I restore the connection to the Rails env. 刚获取数据后,我就恢复了与Rails env的连接。
(My guess is that Passenger spawns multiple processes therefore this should not be a problem) (我的猜测是乘客产生了多个过程,因此这应该不是问题)
On a normal pasenger deploy this is not a problem at all. 在普通的Pasenger部署上,这根本不是问题。 It indeed just spawns multiple processes.
实际上,它只是产生了多个进程。 They do not share any state, so there cannot be any side effects.
它们不共享任何状态,因此不会有任何副作用。
If you would use passenger enterprise in a multithreaded setup this could be some concern. 如果要在多线程设置中使用客运企业,则可能要担心一些。 But establish_connection, to my knowledge, takes care of grabing a new connection from the connection pool and saves it into Thread.current hash for this Class.
但是据我所知,Establishment_connection负责从连接池中获取一个新连接,并将其保存到该Class的Thread.current哈希中。 So there should be also no side effects, like having the connection globally switched for other requests.
因此也应该没有副作用,例如将连接全局切换为其他请求。
Take a further look at Concurrency and Database Connections with ActiveRecord 进一步了解ActiveRecord的并发和数据库连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.