简体   繁体   English

MySQL 在并行 Cron 任务期间连接关闭

[英]MySQL Connection Closing During Parallel Cron Tasks

I have written a Zend Framework based cron service for parallel tasks based on these two blog articles:我根据这两篇博客文章为并行任务编写了一个基于 Zend Framework 的 cron 服务:

In summary, the cron services uses pcntl_fork() to spawn the tasks in parallel.总之,cron 服务使用pcntl_fork()来并行生成任务。

Running a single task with the service works without issues, but when I add a second task, I get this MySQL error:使用该服务运行单个任务没有问题,但是当我添加第二个任务时,我收到此 MySQL 错误:

General error: 2006 MySQL server has gone away一般错误:2006 MySQL 服务器已消失

My best guess is that a child thread ends before the other and the MySQL connection is implicitly closed.我最好的猜测是一个子线程在另一个之前结束,并且 MySQL 连接被隐式关闭。 If this is the case, how do I ensure that the connection stays open until the parent thread closes?如果是这种情况,我如何确保连接保持打开状态,直到父线程关闭?

After reading the comments on pcntl_fork() and this SO question , it was indeed the issue with children sharing the parent connection.在阅读了关于pcntl_fork()的评论和这个SO question之后,确实是孩子共享父连接的问题。 I have added this code to create a new MySQL connection after forking, and it seems to have fixed the problem:我已添加此代码以在分叉后创建新的 MySQL 连接,它似乎已经解决了问题:

// give this thread its own db connection
$settings = Zend_Registry::get('settings');
$db = Zend_Db::factory(
    $settings->db_adapter,
    array(
        'host' => $settings->db_host,
        'username' => $settings->db_user,
        'password' => $settings->db_pass,
        'dbname' => $settings->db_name,
    )
);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Db_Table::setDefaultAdapter($db);

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

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