php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36602 "MySQL server has gone away" with PDO::ATTR_PERSISTENT => true
Submitted: 2006-03-03 11:26 UTC Modified: 2006-04-09 08:12 UTC
Votes:8
Avg. Score:4.8 ± 0.7
Reproduced:8 of 8 (100.0%)
Same Version:8 (100.0%)
Same OS:1 (12.5%)
From: nerve at gmx dot net Assigned:
Status: Closed Package: PDO related
PHP Version: 5.1.2 OS: Gentoo Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
16 + 50 = ?
Subscribe to this entry?

 
 [2006-03-03 11:26 UTC] nerve at gmx dot net
Description:
------------
My Site connect with PDO and PDO::ATTR_PERSISTENT => true to MySQL.
If the MySQL server is restarting or a tread of the Server is killed, the Apache tread wich is connected cant execute any statements until Apache was restartet.
The PDO Connect return with succes, but the first query fails with "MySQL server has gone away". 
A MySQL Server restart was no problem with the old mysql_pconnect function.

I want a detection on creation of new PDO object with persistent connection if the connection is alive or a method to close the old connection.

I use php 5.1.2 with apache 2.0.55 on gentoo linux with mysql 5.0.18-log

Please se my workaround code, witch make a fallback to slower non persistent object.

Reproduce code:
---------------
// Workaround code for problem with pdo and persistent connection
try {
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_db, $db_user,$db_pw, array(PDO::ATTR_PERSISTENT => true));
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db_info = $db->getAttribute(PDO::ATTR_SERVER_INFO); //  erkennung von toten persistenten verbindungen ...
if($db_info == "MySQL server has gone away")
{
  $db = null;
  $db = new PDO('mysql:host='.$db_host.';dbname='.$db_db, $db_user,$db_pw);
}
}catch (PDOException $e) 
{
   print "Fehler in der DB Verbindung!: " . $e->getMessage() . "<br/>";
   $db = null;
   die();
}


Expected result:
----------------
alive persistent connection if mysql is running !

Actual result:
--------------
dead mysql persistent connection if mysql was restartet !!!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-03 15:32 UTC] georg@php.net
Libmysql client library doesn't allow reconnect by default since version 5.0.3 (Also mysql_pconnect shouldn't work after restarting MySQL server).
 [2006-03-04 09:42 UTC] nerve at gmx dot net
That's right, after installing the new mysql i havent testet it. 
But what to do ? I can't explicitli close the persistent connections until apache is restartet. 
Same problem happen if mysql timeout is reached.

Can we make a solution for the php user how don't want to programm a workaround to normal connections ? (Transparent solution in php source code ? )

Thank's for your help.
 [2006-03-04 22:04 UTC] georg@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

As this is documented clearly in mysql documentation -> bogus.
 [2006-03-04 23:56 UTC] nerve at gmx dot net
Thank you for your good help ;-(,
clearly it's in the docu,
but in this case persistent connections are useles ;-(
 [2006-03-08 15:48 UTC] nerve at gmx dot net
And what is abaut a new method 
to destroy the death persistent object ?
So every user can destroy it and recreate the persistent connection without restarting the webserver ?

Not all php users have the choise to restart the webserver if a persistent connection is going to hell ?

Thank you
Daniel
 [2006-03-08 15:54 UTC] nerve at gmx dot net
Or an other idea, how about the posibility to specify a time after which the persistent connection is closed ? (Aka timeout).

I want beleve that mysql is the only pdo backend 
with useles persistent connections.

I (and every other ho is using persistent connections for a big or important aplication) thank you for a good solution.

Daniel Heule
 [2006-03-13 15:47 UTC] damien dot harper at gmail dot com
I'm running a debian box dual Xeon 2 gigs of RAM and powering Apache 2.2.0, PHP 5.1.2, MySQL 5.0.18, PDO_mysql 5.0.18 and getting the same issues.

It would be really nice to be able to set the persistent connections lifetime.

Otherwise, PDO should be smart enough to autodetect dead connections and avoid to return them.
 [2006-04-09 08:12 UTC] wez@php.net
Fixed in CVS.
 [2016-03-04 11:35 UTC] source dot spider at gmail dot com
It's 2016 and this is still broken.
 [2020-02-11 16:56 UTC] rgagnon24 at gmail dot com
It's 2020 and this is still a thing in 7.4.x
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Dec 04 18:03:34 2021 UTC