简体   繁体   English

如果我将一个进程分解为ruby,我是否需要重新连接到Mysql DB?

[英]If I Fork a process in ruby, do I need to reconnect to Mysql DB?

I'm using the following environment. 我正在使用以下环境。

  • Rails 3.1 Rails 3.1
  • Unicorn 独角兽
  • Mysql Mysql的

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.

相关问题 如何ping MySQL数据库并使用PDO重新连接 - How do i ping the MySQL db and reconnect using PDO 我需要规范化此MySQL数据库吗? - Do I need to normalize this MySQL db? 在pcntl_fork之后,如何在父进程中保持mysql连接? - How do I keep my mysql connection in the parent process after pcntl_fork? 为什么我需要重新连接到数据库才能查看表数据的变化? - Why do I need to reconnect to the database to see changes in table data? 如果我在 MySQL 中有设计的数据库,是否需要创建数据库迁移? - Do I need to create DB migration, if I have a designed DB in MySQL? 如果更改MySQL数据库的字符集,是否需要修改Ruby客户端? - If I change the character set of a MySQL database, do I need to modify Ruby clients? 如果我正在与远程mysql数据库建立安全连接,是否需要在两台服务器上都有证书 - Do I need a certificate on both servers if I'm making a secure connection to a remote mysql db 在数据库中保存数据时,真的需要使用mysql_real_escape_string吗? - Do I really need to use mysql_real_escape_string when I save data in the DB? 我应该经常断开/重新连接MySQL服务器吗? - Should i disconnect/reconnect to MySQL server often? 使用Solr,我还需要一个SQL数据库吗? - With Solr, Do I Need A SQL db as well?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM