Why to close a mysql connection?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Why to close a mysql connection?

Post by klevis miho »

Why should I close a mysql connection?
Is it necessary to close it?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Why to close a mysql connection?

Post 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
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Why to close a mysql connection?

Post by klevis miho »

Thnx pytrin.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Why to close a mysql connection?

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Why to close a mysql connection?

Post by Eran »

There is no "intelligence" or "figuring out things". When a script finishes executing, it releases all the resources it used.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Why to close a mysql connection?

Post 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.
heronfisher
Forum Newbie
Posts: 4
Joined: Thu Mar 11, 2010 3:52 am

Re: Why to close a mysql connection?

Post 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..
Post Reply