Should I $ mysqli-> close the connection after loading each page, if PHP is running through FCGI?

I run PHP through FCGI - this is my web server that spawns several PHP processes, and they continue to work as 10,000 requests until they are processed.

My question is: if I have $mysqli->connect at the top of my PHP script, do I need to call $mysqli->close when I'm going to finish running the script?

Since PHP processes have been open for a long time, I would like every $mysqli->connect to be able to make 1 connection, because the process continues to work and no one closes the connection.

Am I right in my thoughts or not? Should I call $mysqli->close ?

+6
source share
6 answers

When PHP exits, it gracefully closes the database connections.

The only reason for using the close method is that you want to end the connection to the database, which will no longer be used, and you have a lot to do: how to process and stream data, but if it's fast, you can forget about the closed application.

Putting it at the end of a script means redundancy, lack of performance, or increased memory.

+7
source

In more detail, in particular about FastCGI:

FastCGI supports PHP processing between requests. FastCGI is good at reducing CPU usage by using your RAM on the server to store PHP scripts in memory instead of running a separate PHP process for each PHP request.

+3
source

FastCGI will launch the master process and as many forks of this master process as you have defined, and yes, these branched processes can work for a long time. This means that the process does not have to run the full PHP process every time it needs to execute a script. But that's not how you think your scripts are now working all the time. The start and stop phase is still executed every time a script is to be executed. At the moment, things like global variables (e.g. $_POST and $_GET ) are populating, etc. You can execute functions every time your process terminates with register_shutdown_function() .

If you do not use persistent connections to the database and do not close connections to the database, nothing bad will happen. As Colin Sean explained, PHP eventually closes them during shutdown.

However, I highly recommend that you close your connections, because a properly created program knows when the object's lifetime has expired and is cleared. This can give you exactly the milliseconds or nanoseconds that you need to deliver on time.

It's easy to always create standalone objects that are also cleaned up after they are finished with what they have done.

+3
source

I never trusted FCGI to close my database connections for me. One of the habits that I learned in a beginner book many years ago is to always explicitly close my database connections.

Don't print sixteen keystrokes for possible memory and connection leak? As far as I know, its cheap insurance.

+2
source

If you have been running FastCGI processes for a long time, for example, php-fpm, you can improve performance by reusing the database connection inside each process and avoiding the cost of opening it.

Since you are most likely opening a connection at some point in your code, you should read how to get mysqli to open a persistent connection and return it to you on subsequent requests controlled by the same process.

http://php.net/manual/en/mysqli.quickstart.connections.php

http://php.net/manual/en/mysqli.persistconns.php

In this case, you do not want to close the connection, otherwise you defeat the goal of opening it. Also, keep in mind that each PHP process will use a separate connection so that your database can open at least as many connections at the same time.

+2
source

You are right in your way of thinking. It is still important to close the connection to prevent memory and data leakage and damage.

You can also reduce the number of rewrites of each loop to stop the connection.

For example: every 2500 script starts, stops, and closes and reopens the connection.

Also recommended: back up your data often.

Hope I helped. Phantom

+1
source

Source: https://habr.com/ru/post/976536/


All Articles