Page 1 of 1

Why to close a mysql connection?

Posted: Tue Mar 02, 2010 5:00 am
by klevis miho
Why should I close a mysql connection?
Is it necessary to close it?

Re: Why to close a mysql connection?

Posted: Tue Mar 02, 2010 5:37 am
by Eran
The connection will close when the script ends. Sometimes, for a long running script it is better to close the connection after it is no longer needed to free up resources. For 99% of all cases you shouldn't need to close it manually

Re: Why to close a mysql connection?

Posted: Tue Mar 02, 2010 9:12 am
by klevis miho
Thnx pytrin.

Re: Why to close a mysql connection?

Posted: Fri Mar 05, 2010 2:07 pm
by BDKR
pytrin wrote:The connection will close when the script ends. Sometimes, for a long running script it is better to close the connection after it is no longer needed to free up resources. For 99% of all cases you shouldn't need to close it manually
Right on the money! In a system I recently developed t hat runs from the command line in both Windows and Linux will leak memory if you are not religous about
closing your connections.

My suspicion is that you are most likely better off to tell PHP exactly what you want it to do as opposed to letting it figure it out on it's own. In this case, it's going to make the right choice all the time, but my concern is more related to the associated overhead of the intelligence required to make this kind of thing work.

Re: Why to close a mysql connection?

Posted: Fri Mar 05, 2010 2:53 pm
by Eran
There is no "intelligence" or "figuring out things". When a script finishes executing, it releases all the resources it used.

Re: Why to close a mysql connection?

Posted: Fri Mar 05, 2010 3:12 pm
by BDKR
pytrin wrote:There is no "intelligence" or "figuring out things". When a script finishes executing, it releases all the resources it used.
And of course you're right. I was thinking of other phases of script execution.

Re: Why to close a mysql connection?

Posted: Fri Mar 12, 2010 7:09 am
by heronfisher
When using something like cgi, it's completely unnecessary to close your mysql connections since they close automatically at the end of script execution. When using persistent technologies , which maintain your connections between requests, then it's important to keep track of connections, global variables, etc..