2

I want to explicitly close mysql connection in my php script to prevent too many connections, using below code:

<?php
$db = new PDO('mysql:host=HOST;dbname=DB',USER,PASSWORD);
$sth=$db->query("SHOW TABLES");
$sth=null;
$db = null;
sleep(10);
?>

Without adding $sth=null; in the above code, I am not able to close my mysql connection.

As stated in PDO document,

To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted

To ensure above, by assigning NULL to the variable that holds the object should ideally close my connection. but for the same, I need to destroy pdo statement handler references also.

In my code, I can make null all references of PDO object but to destroy statement handler in the large codebase is very difficult task for me. Any workaround?

Tanu Gupta
  • 602
  • 1
  • 11
  • 26
  • 1
    Is this script running for a long time…? If not, there's little need to close the connection explicitly, since it's going to be discarded when the script ends anyway. – deceze Mar 13 '18 at 10:01
  • Possible duplicate of [PDO closing connection](https://stackoverflow.com/questions/18277233/pdo-closing-connection) – Anthony Mar 13 '18 at 10:02
  • I have certain synchronous activities which take time, resulting in making the connections on sleep. I need to close the connection before those activities – Tanu Gupta Mar 13 '18 at 10:03
  • The third example of the [documentation](http://php.net/manual/en/pdo.connections.php#example-1013) anwsers to your question. – Anthony Mar 13 '18 at 10:05
  • @YourCommonSense, It was different question yesterday. I was debugging the same problem though. I am still not able to see connection quit on mysql logs writing that particular code. – Tanu Gupta Mar 13 '18 at 10:08
  • How it's any different? – Your Common Sense Mar 13 '18 at 10:09
  • @AnthonyB, Its not duplicate of https://stackoverflow.com/questions/18277233/pdo-closing-connection. My application can not manage persistent connection. – Tanu Gupta Mar 13 '18 at 10:09
  • @TanuGupta The question seems to be the same, closing connection with PDO. Also in the answer he explains what says the documentation. He says that setting _null_ to the variable is the right thing to do. The link to the documentation that I've posted above says the same, setting _null_ to the variable and the statement closes the connection. – Anthony Mar 13 '18 at 10:14
  • @YourCommonSense, In that code (Reference https://stackoverflow.com/questions/49236104/mysql-pdo-connection-is-not-closing), I am not using pdo statement handler. Connection is getting closed with that particular code. Its just that I am not able to see that in mysql logs. Though, I can see the connection closed in mysql processlist. – Tanu Gupta Mar 13 '18 at 10:16
  • You *can* see the connection close in the process list, just no entry in the log…? – deceze Mar 13 '18 at 10:18
  • @AnthonyB, this is what am I saying, what is the workaround of the same? Destroying statement handler is a tedious task to do in a very large codebase. What is the alternative solution of closing pdo connection – Tanu Gupta Mar 13 '18 at 10:18
  • @deceze, Please dont get confused, I am referring https://stackoverflow.com/questions/49236104/mysql-pdo-connection-is-not-closing above – Tanu Gupta Mar 13 '18 at 10:20
  • Then please don't confuse us. ;-) I think I am starting to see your point though. – deceze Mar 13 '18 at 10:22
  • @TanuGupta a possible workaround would be to create a class that does the job. It would be a wrapper for PDO, that would have a method to destroy automatically the connection and the statement. – Anthony Mar 13 '18 at 10:23

0 Answers0