What happens if MySQL database is not closed? How do we know if it is closed properly?
I do have a page which has 11 tables on a page..so what I did is I open database on top of page before script starts and close where the scripts(PHP) ends...
the ending is mysql_close($db);...is this fair enough or do I need to give only mysql_close();
I can't say for sure if all PHP/Mysql versions on all server platforms behave the same way. For tcp connections to the database - unless you call mysql_close($db), you'll have a dangling tcp connection just sitting there waiting to be used for half a minute after the script ends. Then it'll just go away on its own.
I can't say if this is PHP's garbage collection taking a full 30 seconds to complete, or if the tcp connection is set to expire after 30 seconds on its own once you call connect.
Mysql_close($db) instantly kills the tcp connection though. So yeah, I'd say always call mysql_close($db) immediately after you no longer need a database connection in your script.
There isn't a point in closing a connection at the end of the script because that is done for you when the script terminates. The only time you should really call the close function explicitly is if you're running a daemon or something and don't want to hold onto a connection for the entire run length.
PDO is considered the modern database access library for PHP (you really should be using this instead of the mysql_* functions, but that's another story) and even that doesn't have a close function, just to drive the point home.
Related
Do I really need to do mysql_close()? Why or why not?
Is there a trigger that closes the link after mysql_connect even if I don't do mysql_close?
According to the documentation:
Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.
Personally, I always like to make sure I pedantically close anything that I open, but it's not required.
In most cases calling mysql_close will not make any difference, performance-wise. But it's always good practice to close out resources (file handles, open sockets, database connections, etc.) that your program is no longer using.
This is especially true if you're doing something that may potentially take a few seconds - for instance, reading and parsing data from a REST API. Since the API call is going over the line, negative network conditions can cause your script to block for several seconds. In this case, the appropriate time to open the database connection is after the REST call is completed and parsed.
To sum up my answer, the two big rules are:
Only allocate resources (file handles, sockets, database connections, etc.) when your program is ready to use them.
Free up resources immediately after your program is done with them.
what's the benefit of closing the link?
The benefit is that you can free the connection to the database, and corresponding resources in the database server, earlier than the PHP request cleanup would do it.
Say for example you query all the data your request will need in the first 20 milliseconds of the request. But then your PHP code spends another 80 milliseconds running code and formatting the results. Which means 80% of the time, the app is holding open a db connection without needing to, and on average, 8 out of 10 connection threads on the db server are idle and using resources.
The manual says:
Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.
So, no, not really. It is helpful to free up resources before attempting an operation that potentially consumes large amounts of resources though, but it probably won't make a big difference.
what;s the benefit of closing the link?
Normally, there is no benefit in closing the link yourself, as it will be automatically closed.
I can only think of a couple of benefits
If your script has a lot of processing to do after it has finished using the database, closing the database link prematurely may help free up a little bit of memory and other resources (such as MySQL connections) while your script continues with other things. This is very unlikely to be an issue in most scripts, since most scripts will terminate pretty quickly after it has finished with the database connection anyway, and the time the connection is held open before the PHP script terminates will be comparatively short.
Completeness and cleanliness of code. It can give you a good feeling, and is generally good code hygiene to close what you have opened, even though in this case it isn't technically required.
I like to close SQL result sets after I'm done using them with either sqlsrv_cancel($result) or mysqli_free_result($result) depending on the type of connection I'm using.
If I need to call exit() before I call either sqlsrv_cancel or mysqli_free_result is $result implicitly cleared from memory?
By the looks of it: yes.
Open non-persistent MySQL connections and result sets are automatically destroyed when a PHP script finishes its execution.
http://php.net/manual/en/mysqli.close.php
Calling exit() or die() stops PHP script execution.
Yes, it does close the connection and clears from memory, except if you've got a persistent database connection.
Persistent connections are links that do not close when the execution of your script ends. When a persistent connection is requested, PHP checks if there's already an identical persistent connection (that remained open from earlier) - and if it exists, it uses it. If it does not exist, it creates the link. An 'identical' connection is a connection that was opened to the same host, with the same username and the same password (where applicable).
You can read more about it at http://php.net/manual/en/features.persistent-connections.php
You should close the connection because it takes a few seconds or even minutes to close the connection automatically. So, if you have a lots of traffic, you should close the connection as fast as you can so other traffic can use it.
I've built a website and has been running for a while now. but suddenly today I received and error that the maximum connections to the DB is reached.
how can I list the open connections and terminate them using PHP order even through the Db manegement server
This problem happens because you have a lot of users opening connections at the same time. Also, Ajax can cause this problem. Once the execution of the page is done, the connection should of been terminated. Make sure you are recycling a single connection per user every time the page opens when you try different things. For instance you do not want to reopen a connection to the DB (passing user and pass) every time you execute a DB query. Open it once and keep using it until the page renders. It could be that your connections are not being terminated properly but I have not run into this problem before; I do know that PHP has the potential to miss manage connections. Contact your host and ask how many connections are allowed to the DB at any one moment and if they can increase it.
Just every time after executing query close(); connection.
mysql_close ([ resource $link_identifier = NULL ] );
mysqli::close ( void );
mysqli_close ( mysqli $link );
The first method is to use PHP as a CGI "wrapper". When run this way, an instance of the PHP interpreter is created and destroyed for every page request (for a PHP page) to your web server. Because it is destroyed after every request, any resources that it acquires.
In a php script, is it guaranteed that once the script/page ends, an opened database connection is closed without any furthur delay ? (keywords in bold)
Or is it true that the connection will hang around open for like a few seconds (or even milliseconds) before closing?
By without any furthur delay, i meant that it would be just as fast as if we were to compare it with explicitly closing the database connections at the last line of the php script of that page.
Depending on the method of connecting to the db, you could always use a close command. That way, you could be sure of when the connection is closed, and know that it is closed when the script is done.
It depends on how you configure mysql. There is an option to keep the connection for the whole session (persistent connection). Normally this should be avoided and I can't think of any usecase now.
I am using PHP to query the MySQL database on my website. Please answer the following questions:
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?
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.
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?
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?
It will automatically close when the PHP script is done running during destruct phase.
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.
See answer no. 2
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).
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.
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.
If you use non-persistent connections all opened MySQL connections will be automatically closed when the PHP script finishes execution.