Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO Exception + Warning on MySQL Has Gone Away?

Tags:

php

mysql

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?

like image 673
Michael Avatar asked Dec 04 '15 22:12

Michael


1 Answers

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:

  • low wait_timeout - solution: ping, reconnect or increase it too
  • large packets - solution: tune max_allowed_packet in my.cfg

PDO 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.

like image 111
kwarunek Avatar answered Nov 15 '22 11:11

kwarunek