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!
is mysql_close() necessary on exception
Moderator: General Moderators
-
adamSpline
- Forum Newbie
- Posts: 11
- Joined: Fri Sep 16, 2011 1:30 pm
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: is mysql_close() necessary on exception
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.
(#10850)
-
adamSpline
- Forum Newbie
- Posts: 11
- Joined: Fri Sep 16, 2011 1:30 pm
Re: is mysql_close() necessary on exception
thanks for the clarification.
-
x_mutatis_mutandis_x
- Forum Contributor
- Posts: 160
- Joined: Tue Apr 17, 2012 12:57 pm
Re: is mysql_close() necessary on exception
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: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!
Code: Select all
<?php
mysql_connect();
//execute queries...
mysql_close();
//render view/html
?>
-
adamSpline
- Forum Newbie
- Posts: 11
- Joined: Fri Sep 16, 2011 1:30 pm
Re: is mysql_close() necessary on exception
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.
-
x_mutatis_mutandis_x
- Forum Contributor
- Posts: 160
- Joined: Tue Apr 17, 2012 12:57 pm
Re: is mysql_close() necessary on exception
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.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.
Re: is mysql_close() necessary on exception
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.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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.