Page 1 of 1
is mysql_close() necessary on exception
Posted: Sun Apr 29, 2012 12:56 pm
by adamSpline
Hi all,
I was putting code in my php scripts to handle closing mysql connections when there might be an exception or error in the php script. But, then I read in the docs that: "Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution"
Does this mean that the mysql connections will be closed when a script ends normally? Or will it still close the connections even if there is an error or exception within the script? I apologize if this is a silly question. I come from Java and always tried to close connections manually through exception catch clauses. But it seems that this might not be necessary in php. Any clarification will be great. thanks!
Re: is mysql_close() necessary on exception
Posted: Sun Apr 29, 2012 1:13 pm
by Christopher
Yes, all connections are closed when a PHP script ends. This is one of the strengths of PHP because each request is a clean slate greatly reducing the need for the memory and resource management code common to long running programs. A goal is to create just what you need each request. This and the Share Nothing idea are a big part of the difference between programming in PHP and other languages.
Re: is mysql_close() necessary on exception
Posted: Mon Apr 30, 2012 12:39 am
by adamSpline
thanks for the clarification.
Re: is mysql_close() necessary on exception
Posted: Mon Apr 30, 2012 10:22 am
by x_mutatis_mutandis_x
adamSpline wrote:Hi all,
I was putting code in my php scripts to handle closing mysql connections when there might be an exception or error in the php script. But, then I read in the docs that: "Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution"
Does this mean that the mysql connections will be closed when a script ends normally? Or will it still close the connections even if there is an error or exception within the script? I apologize if this is a silly question. I come from Java and always tried to close connections manually through exception catch clauses. But it seems that this might not be necessary in php. Any clarification will be great. thanks!
JAVA gives you an ability to pool the JDBC connections, and you basically obtain and release back to the pool. So, if you do not close, you can saturate your pool very fast. In PHP, there is no connection pooling (I'm not sure if you can implement it using persistent connections or not) but if you do then it becomes a necessity to close it. Otherwise when script finishes execution, it will close the connection automatically. However, better practice is to close connection immediately after you finish accessing DB. For example:
Code: Select all
<?php
mysql_connect();
//execute queries...
mysql_close();
//render view/html
?>
Re: is mysql_close() necessary on exception
Posted: Mon Apr 30, 2012 10:36 am
by adamSpline
yes, no doubt we want to close the connection when we are done with it. I was more asking about exceptions and error handling, as we do not want a connection to stay alive even when a script does not execute completely.
Re: is mysql_close() necessary on exception
Posted: Mon Apr 30, 2012 10:42 am
by x_mutatis_mutandis_x
adamSpline wrote:yes, no doubt we want to close the connection when we are done with it. I was more asking about exceptions and error handling, as we do not want a connection to stay alive even when a script does not execute completely.
Thats correct. I can have a logic/code which needs goes into a big loop/sleep on exception (not likely but just an example); So in that case, you would want to close the connection manually rather than wait for the script to finish execution.
Re: is mysql_close() necessary on exception
Posted: Mon Apr 30, 2012 3:10 pm
by pickle
adamSpline wrote:yes, no doubt we want to close the connection when we are done with it. I was more asking about exceptions and error handling, as we do not want a connection to stay alive even when a script does not execute completely.
Even if a script generates an exception, PHP still does proper cleanup at the end of script execution. As far as I know, the ONLY reason to manually close a connection is if the script is going to run for an extended period of time after the connection is no longer required. Otherwise, just let PHP clean it up.