We are getting both a PDOException and warnings. These warnings are driving us crazy.
Warning: PDOStatement::execute(): MySQL server has gone away in /home/Database.php on line 120
Warning: PDOStatement::execute(): Error reading result set's header in /home/Database.php on line 120
Here is the code that does this -- this is just to simulate a connection going away:
$db = new PDO('mysql:dbname=' . $name . ';host=' . $host, $user, $pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$statement = $db->prepare('SET SESSION wait_timeout = 1');
$statement->execute();
sleep(3);
try {
$statement = $db->prepare('SELECT 1');
$statement->execute();
} catch (PDOException $e) {
echo 'Exception! Err #:' . $e->errorInfo[1] . PHP_EOL;
}
EDIT: The question is why does this generate a warning and an exception. The code above just generates both even though we specifically tell PDO to throw exceptions.
The code above makes it happen faster than waiting for our servers default wait_timeout.
EDIT 2: I'm not sure why this was closed. The question is WHY is PHP spawning both a Warning, and an Exception regardless of the PDO Error Level?
You set wait_timeout
to 1 then you sleep 3, what will happen? MySql will close connection after one second and you will get error "Mysql Server has gone away" with next statement 'cause you sleep for 3.
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_wait_timeout
edit
Question is duplicate of MySQL error 2006: mysql server has gone away
edit 2
Causes of this error:
max_allowed_packet
in my.cfgPDO reconnect - simulate ping in PDO How do i ping the MySQL db and reconnect using PDO
edit 3 question updated
The only way (afaik) to get rid of these warnings is to set expected (eg. E_ERROR) error_reporting level. You could wrapped pdo calls up in for example to set E_ERROR before and reset to default after execution.
PDO logs warnings/errors for logs purposes (sic!) for further analytics. The attribute you set (by setAttribute or constructor) only changes error handling/behavior of pdo - throw or not:). These two things are separated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With