MySQL database connection is not closed: what will happen?

I am using PHP to query the MySQL database on my website. Answer the following questions:

  • What happens if I do not use mysql_close() when I am done with the database query at the end? Will the connection remain open? If so, what time is it? If not, why?
  • If I open and close the connection to MySQL in several places in a web page, how does performance affect? that is, the connection is made again each time when some access to the database on one web page is required.
  • How is mysql_close() related to performance? Do I have to open a new connection every time I need some access to the database, or should I save only one connection and close it at the end?
  • If I do not close the connection, then if the user tries to access some data again, will the new connection be used or the old open connection will be used?
+7
source share
4 answers
  • It will be automatically closed when the PHP script is launched during the destruct phase.
  • Performance will adversely affect. Opening a new socket (especially on an external database server) is more expensive and time consuming than just storing a pointer to the current connection in memory.
  • See answer no. 2
  • Data will be accessed using a new PHP request. Therefore, you will have a new database connection. There are no problems.

I would advise opening a database connection during the build phase, reusing that connection during the entire execution of your script (if it is based on OO, assign a class variable to connect to the database and use $this->db during the whole script) , and close it during the destruction (or do not worry when closing it, because it will be closed in any case, even if it is not announced specifically).

+8
source

From php.net:

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

for performance, it depends on situations, how long it has been used, how long it has been idle, etc. (e.g. long run). In most cases, there is a singleton pattern by which you have one open connection, and do all the requests using this open descriptor. But this is not entirely true, since mysql_connect itself is support:

If the second call is made using mysql_connect () with the same arguments, the new connection will not be established, but instead the link identifier for the already opened link will be returned. The new_link parameter changes this behavior and forces mysql_connect () to always open a new link, even if mysql_connect () was called earlier with the same parameters.

Basically, mysql_close is not really needed when it comes to short scripts.

+2
source

There is a slight performance loss to close the connection compared to using resources to keep it open when you don't need it. It’s better to close the connection as soon as you are done with it, and open it again when you need to.

+1
source

If you use non-persistent connections, all open MySQL connections will be automatically closed when the PHP script completes execution.

+1
source

All Articles