mysqli_pconn_reuse.phpt 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. --TEST--
  2. mysqli_pconnect() - reusing/caching persistent connections - TODO
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. die("skip TODO - we need to add a user level way to check if CHANGE_USER gets called by pconnect");
  8. require_once('skipifconnectfailure.inc');
  9. ?>
  10. --INI--
  11. mysqli.allow_persistent=1
  12. mysqli.max_persistent=2
  13. mysqli.max_links=2
  14. --FILE--
  15. <?php
  16. require_once("connect.inc");
  17. $host = 'p:' . $host;
  18. if (!$link1 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
  19. printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
  20. $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
  21. }
  22. if (!mysqli_query($link1, 'SET @pcondisabled = "Connection 1"'))
  23. printf("[002] Cannot set user variable to check if we got the same persistent connection, [%d] %s\n",
  24. mysqli_errno($link1), mysqli_error($link1));
  25. if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
  26. printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
  27. $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
  28. }
  29. if (!$res = mysqli_query($link1, 'SELECT @pcondisabled AS _test'))
  30. printf("[004] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
  31. $row = mysqli_fetch_assoc($res);
  32. printf("Connection 1 - SELECT @pcondisabled -> '%s'\n", $row['_test']);
  33. mysqli_free_result($res);
  34. if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
  35. printf("[005] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
  36. $row = mysqli_fetch_assoc($res);
  37. printf("Connection 2 (no reuse) - SELECT @pcondisabled -> '%s'\n", $row['_test']);
  38. $thread_id = mysqli_thread_id($link2);
  39. printf("Connection 2 (no reuse) - Thread ID -> '%s'\n", $thread_id);
  40. mysqli_free_result($res);
  41. if (!mysqli_query($link2, 'SET @pcondisabled = "Connection 2"'))
  42. printf("[006] Cannot set user variable to check if we got the same persistent connection, [%d] %s\n",
  43. mysqli_errno($link2), mysqli_error($link2));
  44. if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
  45. printf("[007] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
  46. $row = mysqli_fetch_assoc($res);
  47. printf("Connection 2 - SELECT @pcondisabled -> '%s'\n", $row['_test']);
  48. mysqli_free_result($res);
  49. mysqli_close($link2);
  50. /* reuse of existing persistent connection expected! */
  51. if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
  52. printf("[008] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
  53. $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
  54. }
  55. if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
  56. printf("[009] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
  57. $row = mysqli_fetch_assoc($res);
  58. printf("Connection 2 (reuse) - SELECT @pcondisabled -> '%s'\n", $row['_test']);
  59. $thread_id_reuse = mysqli_thread_id($link2);
  60. printf("Connection 2 (reuse) - Thread ID -> '%s'\n", $thread_id_reuse);
  61. mysqli_free_result($res);
  62. if ($thread_id != $thread_id_reuse)
  63. printf("[010] Seems as if we have got a new connection, connections should have been cached and reused!\n");
  64. mysqli_close($link1);
  65. mysqli_close($link2);
  66. print "done!";
  67. ?>
  68. --EXPECTF--
  69. Connection 1 - SELECT @pcondisabled -> 'Connection 1'
  70. Connection 2 (no reuse) - SELECT @pcondisabled -> ''
  71. Connection 2 (no reuse) - Thread ID -> '%d'
  72. Connection 2 - SELECT @pcondisabled -> 'Connection 2'
  73. Connection 2 (reuse) - SELECT @pcondisabled -> 'Connection 2'
  74. Connection 2 (reuse) - Thread ID -> '%d'
  75. done!