pcntl_fork and the MySQL connection is gone pcntl_fork and the MySQL connection is gone php php

pcntl_fork and the MySQL connection is gone


(comment --> answer per poster's request)

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


This helped for me: http://www.electrictoolbox.com/mysql-connection-php-fork/

Especially mysql_connect($server, $username, $password, true);


You can avoid closing connection when forked process exit, if you kill forked process with SIGKILL.

<?php$dbh = new PDO('pgsql:host=localhost', $username, $password);$pid = pcntl_fork();if($pid == 0){        register_shutdown_function(function(){                posix_kill(getmypid(), SIGKILL);        });        exit;}sleep(1);$statement = $dbh->query('select 1');var_dump($statement);

The reason of this behavior, that when PHP process is exit, than PHP sends to database server "Terminate connection" command. But socket will be closed by system only when all links to socket is closed. Using SIGKILL help us to avoid sending "Terminate connection" command to database server.