問題描述
我有一個 foreach 循環,它在其中分叉.在進程 fork 之后,它訪問數據庫.我收到一個錯誤:
I have a foreach loop that forks within it. After the process forks, it accesses the database. I get an error:
SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
問題是,我正在分叉之后連接到數據庫.
The thing is, I'm connecting to the database after I've forked.
我的問題:為什么會發生這種情況?
My question: Why would this be happening?
如果發生這種情況,我是否真的在分叉之前訪問了數據庫??孩子會繼承數據庫連接嗎?
If this happens, am I actually accessing the database before forking? Will the child inherit DB connections?
(注意:我可以發布代碼,但它相當大,因為它都在類中,這可能是導致我在訪問數據庫時感到困惑的原因.您應該知道的另一件事是我正在使用 ZF.)
(note: I can post code, but it's rather large as it's all in classes, which could be what is causing my confusion of when I'm accessing the DB. Another thing you should know is I'm using ZF.)
推薦答案
(comment --> answer per poster's request)
(comment --> answer per poster's request)
閱讀更多內容我看到分叉的孩子確實繼承了他們父母的數據庫連接,這是一個已知問題:http://php.net/manual/en/function.pcntl-fork.php#70721
Reading more into it I see forked children do inherit their parent's db connection, and it is a known problem: http://php.net/manual/en/function.pcntl-fork.php#70721
這篇關于pcntl_fork 和 MySQL 連接消失了的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!