Threading can work, DB connection unterminated:
I'm not having the same issue that you are when I switch
over $pid
-
<?php
require('app/Mage.php');
Mage::app();
$pid = pcntl_fork();
switch($pid) {
case -1:
print "Could not fork!\n";
exit;
case 0:
echo "In Child " . Mage::getModel('catalog/product')->getCollection()->getLastItem()->getId() . PHP_EOL;
break;
default:
echo "In Parent " . Mage::getModel('catalog/product')->getCollection()->getFirstItem()->getId() . PHP_EOL;
}
Returns:
> php test.php
In Parent 1
In Child 32
However if I do not, I get a MySQL gone away error:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query
Summary:
I think you have an error in your execution code somewhere wherein you are not executing database queries from within the switch over process ID.
Edit
Further investigation:
By declaring a Mage::registry
value, and then inspecting the source of the query we can, from within lib/Varien/Db/Adapter/Pdo/Mysql.php
, inspect the scope (pid) of the current running process:
test.php:
$pid = pcntl_fork();
Mage::register('pcntl',$pid);
lib/Varien/Db/Adapter/Pdo/Mysql.php:
public function query($sql, $bind = array())
{
$pid = Mage::registry('pcntl');
switch($pid){
case 0:
echo "In child" . PHP_EOL;
case 1:
echo "In parent!" . PHP_EOL;
}
//.... stuff
Result:
In child
In parent!
In child
In parent!
In child
In parent!
In child
In parent!
In child
In parent!
In child
In parent!
In child
In parent!
In child
In parent!
In child
In parent!
In child
In parent!
You need to make the user sitename@localhost
able to access the database and tables in question. Use the following SQL as a root or superuser on the MySQL instance to grant the privileges:
GRANT ALL ON magento_local.* TO 'sitename'@'localhost';
For more information on Grants in MYSQL see the manual: http://dev.mysql.com/doc/refman/5.1/en/grant.html
Best Answer
Yes, you can. Assuming you open the connection with something like this:
You can close the connection with this:
And then re-open it with this:
When I need to make an api call that could take a long time, I close the connection first, then use getConnection to open it again afterwards, which has solved the "mysql server has gone away" problem for me.