|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-09-20 17:13 UTC] marion dot jeff at gmail dot com
Description:
------------
In mysql_driver.c (1.0.2) there is an #ifdef MYSQL_OPT_RECONNECT, to check for the availability of the option to enable auto-reconnects, which never matches. The reconnect option changed from default on to off with 5.0.3 and the MYSQL_OPT_RECONNECT option was added to re-enable it with 5.0.13.
So since mysql 5.0.3 pdo_mysql connections which are timed out by a wait_timeout on the server are unusable without a manual reconnect. The intention of the code is to force auto-reconnects when available and is commented as such.
The mysql_options are enums defined in mysql.h and are not available to the preprocessor in mysql_driver.c. In 1.0.2 this is the only issue caused by an ifdef on a MYSQL_OPT_* but in newer branches there is an at least one additional broken check.
Reproduce code:
---------------
/etc/my.cnf
[mysqld]
wait_timeout = 30
<?php
$conn = new PDO('mysql:host=localhost', 'user', 'pass');
$a = $conn->query('SELECT NOW(), 1');
var_dump($a->fetchAll(PDO::FETCH_ASSOC));
sleep(40);
$b = $conn->query('SELECT NOW(), 2');
var_dump($conn->errorInfo());
var_dump($b->fetchAll(PDO::FETCH_ASSOC));
?>
Patch for mysql_driver.c (also takes in to account a mysql bug between 5.0.13 and 5.1.6 where mysql_real_connect reset OPT_RECONNECT to off):
483c483
< #ifdef MYSQL_OPT_RECONNECT
---
> #if MYSQL_VERSION_ID >= 50013 && MYSQL_VERSION_ID >= 50106
485c485
< * we want the old behaviour */
---
> * we want the old behaviour. starting with 5.1.6 set pre-connect. */
535a536,543
> #if MYSQL_VERSION_ID >= 50013 && MYSQL_VERSION_ID < 50106
> /* before 5.1.6, MYSQL_OPT_RECONNECT gets reset by mysql_real_connect */
> {
> long reconnect = 1;
> mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect);
> }
> #endif
>
Expected result:
----------------
array(1) {
[0]=>
array(2) {
["NOW()"]=>
string(19) "2009-09-20 21:05:58"
[1]=>
string(1) "1"
}
}
array(1) {
[0]=>
string(5) "00000"
}
array(1) {
[0]=>
array(2) {
["NOW()"]=>
string(19) "2009-09-20 21:06:38"
[2]=>
string(1) "2"
}
}
Actual result:
--------------
array(1) {
[0]=>
array(2) {
["NOW()"]=>
string(19) "2009-09-20 21:07:04"
[1]=>
string(1) "1"
}
}
array(3) {
[0]=>
string(5) "HY000"
[1]=>
int(2006)
[2]=>
string(26) "MySQL server has gone away"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 21 04:00:02 2025 UTC |
I've got the same problem,too. PDO class does not have a reconnect function.When reading the source, I found the broken code. So I have to make some change as below: mysql_driver.c: ------------- 594 (added) long reconnect = pdo_attr_bval(driver_options, PDO_MYSQL_ATTR_RECONNECT, 0 TSRMLS_CC); 595 639 //#ifdef MYSQL_OPT_RECONNECT 640 /* since 5.0.3, the default for this option is 0 if not specified. 641 * we want the old behaviour */ 642 if( reconnect ){ 643 long reconnect = 1; 644 mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect); 645 } 646 //#endif php_pdo_mysql_int.h ------------------- 159 PDO_MYSQL_ATTR_INIT_COMMAND, (added) MYSQL_OPT_RECONNECT, pdo_mysql.c ----------- 72 REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_INIT_COMMAND", (long)PDO_MYSQL_ATTR_INIT_COMMAND); (added) REGISTER_PDO_CLASS_CONST_LONG("MYSQL_OPT_RECONNECT", (long)PDO_MYSQL_OPT_RECONNECT); implemention of function pdo_attr_bval:(in pdo/php_pdo_driver.h) -------------- static inline long pdo_attr_bval(zval *options, enum pdo_attribute_type option_name, long defval TSRMLS_DC) { zval **v; if (options && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(options), option_name, (void**)&v)) { convert_to_boolean_ex(v); return Z_BVAL_PP(v); } return defval; } after make&install, you can use pdo_options to set MYSQL_OPT_RECONNECT: $conn = new PDO('mysql:host=xxx', 'xx', 'xx',array(PDO::MYSQL_OPT_RECONNECT=>true)); hope this will help.