Script code:
<?PHPTry { $DBH=NewPDO (' Mysql:host=localhost;dbname=test ', ' root ', ' root ');} Catch(pdoexception$e) { Exit(' Connection database failed 1 ');} finally {Echo"Connection succeeded 1\n";}Try { $conn=NewPDO (' Mysql:host=localhost;dbname=test ', ' root ', ' root ');} Catch(pdoexception$e) { Exit(' Connection database failed 2 ');} finally {Echo"Connection succeeded 2\n";}Try { $pdo=NewPDO (' Mysql:host=localhost;dbname=test ', ' root ', ' root ');} Catch(pdoexception$e) { Exit(' Connection database failed 3 ');} finally {Echo"Connection succeeded 3\n";}Echo"Keep connected ... \ n";Sleep(10);Echo"Execution end \ n";
CLI execution:
[Email protected]:/home/www/wenda/webroot# php index.php Connection succeeded 1 connection succeeded 2 connection succeeded 3 remain connected ... End of execution
During the script sleep process, view the MySQL connection information:
Mysql>show Processlist;+----+------+-----------+------+---------+------+----------+------------------+|Id| User |Host|Db|Command|Time|State|Info|+----+------+-----------+------+---------+------+----------+------------------+| 3 |Root|localhost| NULL |Query| 0 |Starting|Show Processlist|| - |Root|localhost|Test|Sleep| 6 | | NULL || - |Root|localhost|Test|Sleep| 6 | | NULL || - |Root|localhost|Test|Sleep| 6 | | NULL |+----+------+-----------+------+---------+------+----------+------------------+4Rowsinch Set(0.00Sec
You can see that the execution of a script results in three database connections, but if you assign the subsequent instantiated PDO instance to a previously instantiated PDO instance, the new connection replaces the previous connection without generating a new connection. So we should avoid instantiating PDO multiple times during the programming process, resulting in unnecessary database performance consumption.
Solution:
1. Encapsulates a class for a singleton pattern that is instantiated by the process of creating a PDO connection. When we set up a database connection, instead of manually instantiating the PDO, we went to get an instance of the class.
2. When instantiating the PDO class, set the persistent connection parameters:
<? PHP $DBH New $user $pass Array ( PDOtrue)); >
References to official documents:
Many Web applications benefit from using persistent connections to the database service.
persistent connections are not closed and cached when the script ends, and are reused when another script connects requests that use the same credentials. Persistent connection caching avoids the overhead of creating a new connection each time the script needs to respond to the database, making the Web application faster.
- What the authorities say
脚本结束
, in FPM mode, is the 一次客户端请求的结束
point. 另一个使用相同凭证的脚本
can also correspond to. 另一个使用相同数据库连接凭证的客户端请求
First of all, we need to know that these two client requests are assigned to a worker to perform according to Fpm-workers's idle situation, so two requests are less likely to be assigned to the same worker. Next, we illustrate the following scenario. 2. After a persistent connection is enabled, the database connection is cached in the FPM process. If a persistent connection is already cached in a fpm-worker process, there are two possible scenarios:
- When a PDO connection with the Attr_persistent parameter is executed again in the script, the previous connection is reused without creating a new connection.
- When a PDO connection without the Attr_persistent parameter is executed again in the script, a new database connection is also generated.
A PHP script executes multiple instances of PDO, which establishes multiple database connections.