Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL database connection not closed: what will happen?

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

  1. What will happen if I don't use mysql_close() when I am done with querying the database in the end? The connection will remain open? If yes then upto how much time? If no then why?
  2. If I open and close a connection to MySQL at several places in a webpage, how is the performance affected? i.e. connection is made again everytime some access to database is required on a single webpage.
  3. How is mysql_close() related to performance? Should I open a new connection everytime some access to database is required OR should I keep only one connection and close it in the end?
  4. If I don't close the connection, then if the user is trying to access some data again, will the new connection be used or the old open connection will be used?
like image 237
sumit Avatar asked Jul 24 '11 19:07

sumit


People also ask

What happens if I open a database connection and not close it?

If you open the connection and don't close it, then it would decrease the connection pools and limits available for connecting to database again. It is always recommended to close the connection and data reader objects explicitly when you use them. Please Sign up or sign in to vote.

Why does MySQL_connect() always open a new link?

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.

What is threads_connected in mysq?

This means that it does not represent current open connections, but all connection attempts since the server has been started. The variable showing number of current open connections, again according to MySQ docs, is Threads_connected.

What does the MySQL connections status variable mean?

According to MySQL documentation the Connections status variable shows "The number of connection attempts (successful or not) to the MySQL server." This means that it does not represent current open connections, but all connection attempts since the server has been started.


1 Answers

  1. It will automatically close when the PHP script is done running during destruct phase.
  2. Performance will negatively be affected. Opening a new socket (especially to an external database server) is more expensive and time consuming than just keeping a pointer to the current connection in memory.
  3. See answer no. 2
  4. The access to the data will be performed by a new PHP request. Hence, you will have a new database connection. No problems there.

I'd advise to open your database connection during construct phase, re-use that connection during the entire execution of your script (if it's OO based, assign a class variable for your database connection and use $this->db during the entire script), and close it during destruction (or don't bother at all closing it, as it will be closed anyway, even when not declared specifically).

like image 175
Rem.co Avatar answered Oct 20 '22 04:10

Rem.co