[英]If I Fork a process in ruby, do I need to reconnect to Mysql DB?
I'm using the following environment. 我正在使用以下环境。
I have to fork a process wich generate invoces. 我必须分叉一个生成invoces的进程。 People told me to use
人们告诉我要用
ActiveRecord::Base.connection.reconnect!
Because with some drivers could lost the connection with fork... I looked for some information about that but I'm more confused than before... here comes my doubts: 因为有些驱动程序可能会丢失与fork的连接...我找了一些关于这方面的信息,但我比以前更困惑...这是我的怀疑:
1 What's the difference between using 1使用之间有什么区别
ActiveRecord::Base.connection.reconnect!
and 和
::ActiveRecord::Base.clear_all_connections!
before forking and 在分叉之前
::ActiveRecord::Base.establish_connection
as shown here? 如图所示? Mysql, using fork in Ruby
Mysql,在Ruby中使用fork
2 Should I do it always? 我应该一直这样做吗?
If the answer is YES
there is some place to put it as "fork configuration"? 如果答案是
YES
有一些地方可以把它作为“分叉配置”吗?
3 what's happen with the connection when the forked process ends? 3分叉进程结束时连接会发生什么? should I close it?
我应该关闭吗? or does it close automatically?
还是会自动关闭? and what's append to the father's process connection?
什么附加到父亲的过程连接?
4 Is it needed for all database connections? 4是否需要所有数据库连接?
I have read somewhere that is only for Postgres DB... 我读过只适用于Postgres DB的地方......
Thank you 谢谢
不,从我的观点来看,你没有必要重新连接..你只需要连接,如果你想访问一个不同的数据库,据我所知rails为自己的mysql处理自己的重新连接。
You need to establish a new connection in your fork even it has no database access. 您需要在fork中建立新连接,即使它没有数据库访问权限。
Because when then forked process exit, it will close the connection it copied, which is the same connection its parent process is using, this may interrupt the parent process. 因为当forked进程退出时,它将关闭它复制的连接,这与其父进程正在使用的连接相同,这可能会中断父进程。 Vise versa.
反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.