Bug #70583 [Com]: mysqlnd drive generates: MySQL server has gone away additional to PDOException
| From: | brandon at roundsphere dot com | Date: | Sun, 09 Apr 2017 04:20:28 +0000 |
| Subject: | Bug #70583 [Com]: mysqlnd drive generates: MySQL server has gone away additional to PDOException | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-208396@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70583&edit=1
ID: 70583
Comment by: brandon at roundsphere dot com
Reported by: s dot chernomorets at gmail dot com
Summary: mysqlnd drive generates: MySQL server has gone away
additional to PDOException
Status: Open
Type: Bug
Package: PDO MySQL
Operating System: linux
PHP Version: 5.6.13
Block user comment: N
Private report: N
New Comment:
I use this as a more easily repeatable test:
<?php
$conf = [
'host' => 'myHostname',
'user' => 'myUsername',
'pass' => 'myPassword',
'port' => 3306,
'name' => 'myDatabaseName',
];
$dbh = new
PDO("mysql:host={$conf['host']};port={$conf['port']};dbname={$conf['name']};",
$conf['user'], $conf['pass']);
$dbh->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
$sql = "SELECT 1";
$sth = $dbh->prepare($sql);
$sth->execute();
$sth->setFetchMode(\PDO::FETCH_NUM);
$row = $sth->fetch();
echo "Got one = {$row[0]}\n";
echo "Setting wait_timeout to 3s\n";
$dbh->query("SET SESSION wait_timeout = 3");
echo "sleeping for 5s\n";
sleep(5);
try {
$sql = "SELECT 1";
$sth = $dbh->prepare($sql);
$sth->execute();
$sth->setFetchMode(\PDO::FETCH_NUM);
$row = $sth->fetch();
echo "Got one = {$row[0]}\n";
} catch (\Exception $e) {
$class = gettype($e);
echo "CAUGHT EXCEPTION {$class}) : ".$e->getMessage()."\n";
}
echo "all done\n";
?>
Expected behavior is that it the output will contain the CAUGHT EXCEPTION line, instead it issues a
PHP Warning and continues to execute. Making it impossible to catch and handle this exception:
Got one = 1
Setting wait_timeout to 3s
sleeping for 5s
PHP Warning: PDOStatement::execute(): MySQL server has gone away in exceptionTest.php on line 30
PHP Warning: PDOStatement::execute(): Error reading result set's header in exceptionTest.php
on line 30
CAUGHT EXCEPTION object) : SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
all done
Previous Comments:
------------------------------------------------------------------------
[2015-09-25 14:55:23] s dot chernomorets at gmail dot com
Description:
------------
In php 5.3 pdo-driver does not generate warnings for error "2006 MySQL server has gone
away", but php 5.6.13 does even when PDO::ATTR_ERRMODE setted to PDO::ERRMODE_EXCEPTION
Test script:
---------------
# cat <<EOF >t.php
<?php
set_error_handler(function($e, $m) {
echo "set_error_handler: $m; " ; var_dump($e);
}, E_ALL & ~E_STRICT);
$pdo = new PDO(
'mysql:host=localhost;port=3306',
'user',
'password'
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$s = $pdo->query('select CONNECTION_ID()');
echo "Connection id: ".$s->fetchColumn()."\n";
try {
$pdo->query('select sleep(100)');
}catch(Exception $e) {
echo "Exception: ".$e->getmessage();
//var_dump($e);
}
EOF
# php t.php &
Connection id: 610
# mysql -h localhost -e 'kill 610'
# We kill mysql thread with id "610" from string "Connection id: 610"
Expected result:
----------------
Exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Actual result:
--------------
set_error_handler: PDO::query(): MySQL server has gone away; int(2)
set_error_handler: PDO::query(): Error reading result set's header; int(2)
Exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70583&edit=1