简体   繁体   English

建立连接和耙任务在一起的问题

[英]issue with establish_connection and rake task together

I have a model and in which i use establish_connection to connect to different database(let name it remote database ) of other application. 我有一个模型,在其中我使用establish_connection连接到其他应用程序的不同数据库(将其命名为remote database )。

example: 例:

class User < ActiveRecord::Base

  establish_connection(some_database_credential_hash)
  self.class_name = "members"

end

and in production environment i am running a rake task which uses User class in it.My question is that if the same rake task runs every hour, will it try to make new connection every time to new members table of remote database increasing load and pool on that database? 在生产环境中,我正在运行一个使用User class的rake任务。我的问题是,如果同一rake任务每小时运行一次,它将每次尝试与远程数据库的新成员表建立新连接,从而增加负载和池在那个数据库上? if Yes, how can i avoid it? 如果是,我如何避免呢? Please suggest. 请提出建议。

This was answered in comments: 这在评论中得到了回答:

  • Anything in the body of a class is run at the point of initialising the environment, which is done fresh each time a rake task is run. 类主体中的所有内容都在初始化环境时运行,每次执行rake任务时都会重新进行。
  • Part of tearing down the rails environment is to close any open connections to databases. 拆除Rails环境的一部分是关闭与数据库的所有开放连接。
  • Unless the rake task is rudely quit the connection established will be closed. 除非不小心放弃了rake任务,否则建立的连接将被关闭。
  • This can be confirmed by logging in to (in this case) mysql and running 可以通过登录(在这种情况下)mysql并运行来确认

    show processlist 显示过程清单

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

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