mysqli_reconnect.phpt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. --TEST--
  2. Trying implicit reconnect after wait_timeout and KILL using mysqli_ping()
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('skipifconnectfailure.inc');
  8. if (stristr(mysqli_get_client_info(), 'mysqlnd'))
  9. die("skip: test for libmysql");
  10. ?>
  11. --INI--
  12. mysqli.reconnect=1
  13. --FILE--
  14. <?php
  15. require_once("connect.inc");
  16. require_once("table.inc");
  17. if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  18. printf("[001] Cannot create second database connection, [%d] %s\n",
  19. mysqli_connect_errno(), mysqli_connect_error());
  20. $thread_id_timeout = mysqli_thread_id($link);
  21. $thread_id_control = mysqli_thread_id($link2);
  22. if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
  23. printf("[002] Cannot get full processlist, [%d] %s\n",
  24. mysqli_errno($link2), mysqli_error($link));
  25. $running_threads = array();
  26. while ($row = mysqli_fetch_assoc($res))
  27. $running_threads[$row['Id']] = $row;
  28. mysqli_free_result($res);
  29. if (!isset($running_threads[$thread_id_timeout]) ||
  30. !isset($running_threads[$thread_id_control]))
  31. printf("[003] Processlist is borked, [%d] %s\n",
  32. mysqli_errno($link2), mysqli_error($link));
  33. if (!mysqli_query($link, "SET SESSION wait_timeout = 2"))
  34. printf("[004] Cannot set wait_timeout, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  35. if (!$res = mysqli_query($link, "SHOW VARIABLES LIKE 'wait_timeout'"))
  36. printf("[005] Cannot check if wait_timeout has been set, [%d] %s\n",
  37. mysqli_errno($link), mysqli_error($link));
  38. if (!$row = mysqli_fetch_assoc($res))
  39. printf("[006] Cannot get wait_timeout, [%d] %s\n",
  40. mysqli_errno($link), mysqli_error($link));
  41. mysqli_free_result($res);
  42. if ($row['Value'] != 2)
  43. printf("[007] Failed setting the wait_timeout, test will not work, [%d] %s\n",
  44. mysqli_errno($link), mysqli_error($link));
  45. // after 2+ seconds the server should kill the connection
  46. sleep(3);
  47. if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
  48. printf("[008] Cannot get full processlist, [%d] %s\n",
  49. mysqli_errno($link2), mysqli_error($link));
  50. $running_threads = array();
  51. while ($row = mysqli_fetch_assoc($res))
  52. $running_threads[$row['Id']] = $row;
  53. mysqli_free_result($res);
  54. if (isset($running_threads[$thread_id_timeout]))
  55. printf("[009] Server should have killed the timeout connection, [%d] %s\n",
  56. mysqli_errno($link2), mysqli_error($link));
  57. if (true !== mysqli_ping($link))
  58. printf("[010] Reconnect should have happened");
  59. if (!$res = mysqli_query($link, "SELECT DATABASE() as _dbname"))
  60. printf("[011] Cannot get database name, [%d] %s\n",
  61. mysqli_errno($link), mysqli_error($link));
  62. if (!$row = mysqli_fetch_assoc($res))
  63. printf("[012] Cannot get database name, [%d] %s\n",
  64. mysqli_errno($link), mysqli_error($link));
  65. mysqli_free_result($res);
  66. if ($row['_dbname'] != $db)
  67. printf("[013] Connection should has been made to DB/Schema '%s', expecting '%s', [%d] %s\n",
  68. $row['_dbname'], $db, mysqli_errno($link), mysqli_error($link));
  69. // ... and now we try KILL
  70. $thread_id_timeout = mysqli_thread_id($link);
  71. if (!mysqli_query($link2, sprintf('KILL %d', $thread_id_timeout)))
  72. printf("[014] Cannot KILL timeout connection, [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
  73. // Give the server a second to really kill the other thread...
  74. sleep(1);
  75. if (!$res = mysqli_query($link2, "SHOW FULL PROCESSLIST"))
  76. printf("[015] Cannot get full processlist, [%d] %s\n",
  77. mysqli_errno($link2), mysqli_error($link));
  78. $running_threads = array();
  79. while ($row = mysqli_fetch_assoc($res))
  80. $running_threads[$row['Id']] = $row;
  81. mysqli_free_result($res);
  82. if (isset($running_threads[$thread_id_timeout]) ||
  83. !isset($running_threads[$thread_id_control]))
  84. printf("[016] Processlist is borked, [%d] %s\n",
  85. mysqli_errno($link2), mysqli_error($link));
  86. if (true !== ($tmp = mysqli_ping($link)))
  87. printf("[017] Expecting boolean/true got %s/%s\n", gettype($tmp), $tmp);
  88. if (!$res = mysqli_query($link, "SELECT DATABASE() as _dbname"))
  89. printf("[018] Cannot get database name, [%d] %s\n",
  90. mysqli_errno($link), mysqli_error($link));
  91. if (!$row = mysqli_fetch_assoc($res))
  92. printf("[019] Cannot get database name, [%d] %s\n",
  93. mysqli_errno($link), mysqli_error($link));
  94. mysqli_free_result($res);
  95. if ($row['_dbname'] != $db)
  96. printf("[020] Connection should has been made to DB/Schema '%s', expecting '%s', [%d] %s\n",
  97. $row['_dbname'], $db, mysqli_errno($link), mysqli_error($link));
  98. mysqli_close($link);
  99. mysqli_close($link2);
  100. print "done!";
  101. ?>
  102. --EXPECT--
  103. done!