MySQL database connection not closed: what will happen?
Asked Answered
A

4

11

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?
Ation answered 24/7, 2011 at 19:52 Comment(0)
S
10
  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).

Stertorous answered 24/7, 2011 at 20:1 Comment(0)
I
2

From php.net :

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.

for performance it depends on situations, how long it is used, for how long it is idle and so on (e.g. long execution). In most cases, there is singleton pattern by which you have one open connection, and make all queries with that open handle. But it's not true all in all, as mysql_connect itself is kind of supports that:

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.

So basically, mysql_close is not very needed when it comes to short running scripts.

Isiahisiahi answered 24/7, 2011 at 20:3 Comment(0)
B
1

There is negligible performance loss to close a connection, compared to the resource usage of keeping it open when you don't need it. Better to close a connection as soon as you're done with it, and open it again when you need to.

Bratcher answered 24/7, 2011 at 19:54 Comment(0)
R
1

If you use non-persistent connections all opened MySQL connections will be automatically closed when the PHP script finishes execution.

Ralaigh answered 24/7, 2011 at 19:57 Comment(1)
how to make my connections non-persistent?Eyler

© 2022 - 2024 — McMap. All rights reserved.