I wonder if I should close any unnecessary database connection inside of my PHP scripts.

I am aware of the fact that database connections are closed implicitly when the block stops executing and 'manually' closing the connections could kinda bloat the codebase with unnecessary code.

But shouldn't I do so in order to make by code as readable and as easy understandable as possible, while also preventing several possible issues during run time?

Also, if I would do, would it be enough to unset() my database object?


For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

Using unset is the same as leaving the database connection open, as you're relying on the garbage collector to clean up after the variable as opposed to asking the connection to gracefully close the connection after you're done with it. Though as mentioned by CodeCaster below, doing so is acceptable since the garbage collector makes sure that the connections are closed.

A useful example, lets say you're eating lunch at a cafeteria somewhere. After eating, you have two choice, throw your left overs in the trash then return the plate/tray to its rightful place(thats cleaning up after yourself). And the other is just leaving your mess on the table and wait for someone else(the garbage collector) to clean it up for you.

If you clean it up yourself, you're sure that your mess is cleaned up. But if you rely on the garbage collector, right before the garbage collector cleans up your mess, what if some weird stalker decides to use your used utensils to aid them in their nightly fantasies about you, then return them back to the cafeteria just in time for the garbage collector to clean up your mess. Stuff like that can happen.

EDIT: Corrected my mistake about garbage collectors.

  • That was one very good and amusing example, i wish there were more of those in some books on the programming topic, especially for younger or older folks. Consider writing one! – Wottensprels Oct 17 '13 at 11:12
  • Thanks, I'm glad you liked the example. Writing a book would be fun but i fear that my experience isn't enough to properly write one as of now. – Maru Oct 17 '13 at 11:37
  • 3
    Explicitly closing connections is not necessary. Closing connections prevents them to be used for connection pooling, so "stalkers" (i.e. other processes requiring a database connection) cannot reuse them. Besides that the documentation guarantees that the connection will be automatically closed, i.e. the cafeteria decides when to clean up, as they know when is the best time to do so. If the garbage collector "forgets" to clean up random variables (like a connection), you have bigger problems; this won't happen. – CodeCaster Oct 17 '13 at 11:41
  • Let's stay with the example. How can one tell if the cafeteria truly knows the best time to clean up, rather than has to spare some free-time to finally clean up that table used 2 hours ago because until now, the waitress was too busy? – Wottensprels Oct 17 '13 at 11:47
  • 2
    Comparisons like that never work. The "waitress" is the garbage collector and doesn't have any other tasks; (s)he is called into action when it is time, not earlier, not later. Again, you can safely assume they know when it is the time to clean up, as this is promised by the documentation. If that table has been dirty for two hours, apparently nobody needed that table. :-) – CodeCaster Oct 17 '13 at 11:49

Yes, it of-course helps. Assume you are building an application, where clients do normal operations with your tables. Now, if the application is heavy and processing intrinsic, it is likely that your application will slow down with vast number of open connections, which will remain idle for a very long duration. If you explicitly ensure that connection is open for the duration a client is performing some action, you may improve on processing power. Besides, its better to clean up mess quickly than to wait for help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.