Using a private database connection in php

I always thought it was always useful to close the database connection, regardless of the database / ORM, e.g. mysql_close (), Propel :: close (), etc.

As for one of my other question and some other research on the Internet, I found out an amazing person that most people actually recommend if you close the connection, since the connection always closes after the request.

However, I find these answers a bit difficult to digest. The reason is why all DB lib, ORM provide a private method? If it is, in every ORM / lib , it should be useful.

Can someone shed some light on under what circumstances should we use a closed method to close a connection to a database? and if these methods are not at all useful, why are they present there in all db libs / ORM?

EDIT

My conclusion

It was a good discussion between Bondye and Fluffeh, and it cleared my doubts about using a close connection. Thanks to both of them.

  • If your script should last less than 100 ms, don't worry about closing the connection.
  • BUT: if the script is expected to last longer and there is free time for the other between the last DB operation and the closing of the script by calling * close ().

It is really very difficult for me to accept one answer, since both answers are correct in their place. Just accept the answer with all the comments so that it stays on top. But +1 for both correct answers.

+7
source share
4 answers

The use of mysql_close() usually not required, as fickle open links automatically close at the end of the script.

Release of resources

Thanks to the reference counting system introduced with PHP 4 Zend Engine, a resource that no longer has links to it is automatically detected and freed by the garbage collector. For this reason, it is rarely necessary to free memory manually.

Hope this helps you more.

( source )

change

mysql_close() also aims to conserve computer resources, but another key reason to use it is that there are a limited number of connections that the MySQL server can accept, and if you have several clients that support connections, the reason where the server might have to cancel other pending clients. Naturally, this is bad, so as with mysql_free_result() , it is useful to call mysql_close() if you think there will be some time between your last use of the database and the end of the script.

+7
source

It is always useful to stop connecting to the database when you no longer need it. Even if it automatically closes after the script ends, it may be another second or a few seconds later. If you no longer need it, one user who gets on the page and loses a connection to the database for half a second will not make any difference - but twenty do it right away is suddenly 10 seconds of an open connection - and it really matters.

At the same time, reusing a connection can be good practice β€” creating and opening a connection usually takes at least a few milliseconds β€” and if you, for example, insert several hundred thousand rows, then it adds up very quickly every few milliseconds.

In a way, this is no different than setting a variable to NULL or canceling it. You don't have to do this, but clean, elegant code and resource management is always good.

+6
source

Database connections are unlimited. Commercial database software, in particular, often has licenses that limit the number of simultaneous connections to a relatively small number. In this situation, you really want to close the connection when your script is no longer actively used. Although PHP automatically closes the database connection when the script exits, it does not do this until the visitor finishes loading the page. If its connection is slow (dial-up or mobile), it may take ten, twenty seconds for everything you know.

+4
source

Well-developed ORMs such as Doctrine and Propel are good at closing MySQL connections. But if you use direct php, I have seen many database problems tracked to open connections. It is advisable to close all db connections at the end of each script.

0
source

All Articles