Jump to content
Nathan

Closing Database Connections

Recommended Posts

Is it important to close your database connection at the end of your PHP file?

 

I assumed it closed as soon as the page was done executing, so what would be the benefit in including it in your code?

Share this post


Link to post
Share on other sites

There are two schools of thought:

 

* Open a connection and then close it immediately when you are done with it.

* Open a connection and keep it open as long as you can.

 

Now they both have pros and cons.

 

I'm biased towards the first option, a database connection is a resource, in a highly transaction environment like a web server. It is best to use those resources in the most efficient manner, opening a connection and not closing it when not using it is like leaving a tap running!

Edited by __Darknite

Share this post


Link to post
Share on other sites

If I am writing a quick script as a utility or something that I know itself won't take up a ton of resources, I won't close connections, but otherwise, I'll throw a mysql_close() in there.

Share this post


Link to post
Share on other sites

I can see several benefits of closing database connections:

 

1) It is considered as a good programming practice

2) It frees up resources that can be used by other processes

3) I'm not sure about PHP but in some languages open connection can be exploited by hackers so closing the connections would make our code more secure.

Share this post


Link to post
Share on other sites

I can see several benefits of closing database connections:

 

1) It is considered as a good programming practice

2) It frees up resources that can be used by other processes

3) I'm not sure about PHP but in some languages open connection can be exploited by hackers so closing the connections would make our code more secure.

 

Actually its only good programming practice provided you are not going to reopen the connection within seconds of closing it down. Otherwise you are actually wasting resources by closing it down.

Share this post


Link to post
Share on other sites

I have read that when the page is finished loading (i.e.  the script ends) the connection closes anyway.  So, unless the page is very intense, it would make sense to not worry too much about it.

Share this post


Link to post
Share on other sites
I have read that when the page is finished loading (i.e.  the script ends) the connection closes anyway.  So, unless the page is very intense, it would make sense to not worry too much about it.

It's a good security measure.

Share this post


Link to post
Share on other sites
I have read that when the page is finished loading (i.e.  the script ends) the connection closes anyway.  So, unless the page is very intense, it would make sense to not worry too much about it.

 

Not every database is on a webpage ;)

Share this post


Link to post
Share on other sites

Adding a mysql_close() is not that big a task and neither it is resource centric - at least on my part as a coder :D. This is one reason, I do add it at the end of the code when I am sure the rest of the code will not need any access to the database. Though I have read on many many places that this is not required and PHP would close the connection (or rather drop the connection) automatically at the end, still, why not do it manually and rest in peace :)

Edited by simplysidy

Share this post


Link to post
Share on other sites

Yes. It shall close automatically when the page is finished executing. But when you are thinking about multiple databases and multiple tables, it does become relevant to close your connection as soon as you are finished with it.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...