Hello Mates,
This is basic query. I have read from php.net manual that mysql automatically destroys a connection with php after the script executes. Also a forum member suggested that it is safer to have many opened connection rather than having unnecessary close resource connection code. Can anyone be kind enough to share his/her views on this? I have this php script from where I do open a lot of connections to mysql and only close it while the application logout (I understand mysql just opens one connection for one source in normal scenerio). Is this the worst possible approach? Can I just trust on my hosting ISP's mysql configuration setting that the connections will be closed automatically, provided they have that setting? Any suggestions would be a great help. Thank you.

    The only reason to leave connections open is if you are using persistent connections. However, they are another area entirely.

    Otherwise, you will want to close connections. Why? Because databases have a maximum number of connections. Therefore is you exceed this you wont be able to connect anymore.

    So the connection will close after the script ends but I assume it will be left open for longer than it needs to be. Perhaps until it times out?

    So, assuming you are using the mysql_* functions. All you need to do is put mysql_close at the end of the pages that use the db.

      However, from http://uk3.php.net/manual/en/language.types.resource.php#language.types.resource.self-destruct

      "Freeing resources

      Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually. "

      so even the PHP manual says you don't really need to do it... So i guess its up to you!

        dougal85 wrote:

        However, from http://uk3.php.net/manual/en/language.types.resource.php#language.types.resource.self-destruct

        "Freeing resources

        Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually. "

        so even the PHP manual says you don't really need to do it... So i guess its up to you!

        Right, by reading the manual on PHP.net I thought it might be ok to leave the connection open. However, I think I would close the connection at the end of my page. Thank you for looking into it and providing some good suggestions.

          Right, by reading the manual on PHP.net I thought it might be ok to leave the connection open. However, I think I would close the connection at the end of my page.

          If all you are going to do is "connection at the end of (your) page", then you might as well leave it open and let PHP close it for you. The value in manually closing the connection is when you know that no more database access is needed, thus you close the connection well before the script interpretation ends.

            Write a Reply...