2009年9月1日星期二

Re: [fw-db] ZendX_Console_Process_Unix, lost DB connection

Thank you for your answer but I can't find a solution. Indeed, the parent
processus needs to have an object to connect to database, so when this
process do the fork() the child also have this object. I think when the
child dies the connection is closed automaticly, no ? The parent process
have always the object db with all the attributes but it can't connect to
db.

I found this in the php doc :
The reason for the MySQL "Lost Connection during query" issue when forking
is the fact that the child process inherits the parent's database
connection. When the child exits, the connection is closed. If the parent is
performing a query at this very moment, it is doing it on an already closed
connection, hence the error.

An easy way to avoid this is to create a new database connection in parent
immediately after forking. Don't forget to force a new connection by passing
true in the 4th argument of mysql_connect():

-------------------------------------------------------------------------------------------------------------
<?
// Create the MySQL connection
$db = mysql_connect($server, $username, $password);

$pid = pcntl_fork();

if ( $pid == -1 ) {
// Fork failed
exit(1);
} else if ( $pid ) {
// We are the parent
// Can no longer use $db because it will be closed by the child
// Instead, make a new MySQL connection for ourselves to work with
$db = mysql_connect($server, $username, $password, true);
} else {
// We are the child
// Do something with the inherited connection here
// It will get closed upon exit
exit(0);
?>

This way, the child will inherit the old connection, will work on it and
will close upon exit. The parent won't care, because it will open a new
connection for itself immediately after forking.
-------------------------------------------------------------------------------------------------------------------------------------

The problem is that I can't do this because it must be done in
ZendX_Console_Process_Unix.

I don't know how to avoid this. Do you have any ideas ?

Thanks again.

Matthieu

Ralph Schindler-2 wrote:
>
> This is a pretty common error when forking (which is what Process_unix
> does).
>
> See: http://us3.php.net/manual/en/function.pcntl-fork.php
>
> You will more than likely have to initiate a new db connection in each
> child.
>
> -ralph
>
> ablsa_matthieu wrote:
>> Version ZF 1.7.1 :
>> Class : ZendX_Console_Process_Unix
>>
>> I create many process unix with the library ZendX_Console_Process_Unix.
>> When
>> the first one is running, I loose the database connection.
>> Indeed, when I access the DB before running the first thread, there is no
>> problem but after I launch the first one this problem occurs :
>> SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
>>
>> Perhaps when the child process is killed, the connection object is also
>> destruct.
>>
>> Do you have any ideas where the problem come from ?
>>
>> Thank you for you consideration.
>>
>> Matthieu
>
>

--
View this message in context: http://www.nabble.com/ZendX_Console_Process_Unix%2C-lost-DB-connection-tp25225029p25237153.html
Sent from the Zend DB mailing list archive at Nabble.com.

没有评论: