首頁 > php教程 > php手册 > 主體

php持久化连接数据库

WBOY
發布: 2016-06-06 19:47:20
原創
1406 人瀏覽過

php的持久化数据库连接已经不是一个新鲜的名词了,由于php语言生命周期的问题,如果每次连接数据库都重新打开一个连接会很低效,所以引入了长连接机制(应该是实现在sapi部分,例如cli sapi就不支持db长连接),本文探究apache2handler sapi和 fpm sapi是否支持db长

php的持久化数据库连接已经不是一个新鲜的名词了,由于php语言生命周期的问题,如果每次连接数据库都重新打开一个连接会很低效,所以引入了长连接机制(应该是实现在sapi部分,例如cli sapi就不支持db长连接),本文探究apache2handler sapi和 fpm sapi是否支持db长连接.使用方法以pdo为例在实例化pdo对象时传入:

<code>PDO::ATTR_PERSISTENT => true
</code>
登入後複製

即可实现长连接,但是隐约在哪里听说过在apache下由于是以线程的方式执行,所以连接在线程关闭时也会释放掉.本着实践是检验真理的唯一标准态度,遂决定做个实验测试下.

测试环境:
mysql-5.1.50
php-5.4.19
apache && nginx

php测试代码:

<code><?php $pdo = new PDO(
    sprintf('mysql:dbname=%s;host=%s;port=%d', 'test', 'localhost', '3306'),
    'test',
    'test',
    array(
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        //PDO::ATTR_PERSISTENT => true #pdo默认为false
    )
);

var_dump($pdo->query('select now()')->fetchColumn());
</code>
登入後複製

测试php-fpm sapi(nginx):
首先查看数据库连接:

<code>mysql> show full processlist;
+----+------+-----------------+------+---------+------+-------+-----------------------+
| Id | User | Host            | db   | Command | Time | State | Info                  |
+----+------+-----------------+------+---------+------+-------+-----------------------+
|  1 | root | localhost:61513 | test | Query   |    0 | NULL  | show full processlist |
+----+------+-----------------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
</code>
登入後複製
登入後複製
登入後複製
登入後複製

访问php测试脚本后查看数据库连接:

<code>mysql> show full processlist;
+----+------+-----------------+------+---------+------+-------+-----------------------+
| Id | User | Host            | db   | Command | Time | State | Info                  |
+----+------+-----------------+------+---------+------+-------+-----------------------+
|  1 | root | localhost:61513 | test | Query   |    0 | NULL  | show full processlist |
+----+------+-----------------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
</code>
登入後複製
登入後複製
登入後複製
登入後複製

将测试脚本中PDO::ATTR_PERSISTENT => true的注释取消掉后执行脚本,再查看数据库连接:

<code>mysql> show full processlist;
+----+------+-----------------+------+---------+------+-------+-----------------------+
| Id | User | Host            | db   | Command | Time | State | Info                  |
+----+------+-----------------+------+---------+------+-------+-----------------------+
|  1 | root | localhost:61513 | test | Query   |    0 | NULL  | show full processlist |
|  3 | test | localhost:61525 | test | Sleep   |    2 |       | NULL                  |
+----+------+-----------------+------+---------+------+-------+-----------------------+
2 rows in set (0.00 sec)
</code>
登入後複製

结论1:fpm sapi支持db长连接

测试apache2handler sapi(apache):
首先关闭掉长连接会话:

<code>mysql> kill 3;
Query OK, 0 rows affected (0.00 sec)
</code>
登入後複製

查看数据库会话列表:

<code>mysql> show full processlist;
+----+------+-----------------+------+---------+------+-------+-----------------------+
| Id | User | Host            | db   | Command | Time | State | Info                  |
+----+------+-----------------+------+---------+------+-------+-----------------------+
|  1 | root | localhost:61513 | test | Query   |    0 | NULL  | show full processlist |
+----+------+-----------------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
</code>
登入後複製
登入後複製
登入後複製
登入後複製

执行测试脚本(注释掉PDO::ATTR_PERSISTENT => true,即未设置长连接的脚本)并查看数据库会话:

<code>mysql> show full processlist;
+----+------+-----------------+------+---------+------+-------+-----------------------+
| Id | User | Host            | db   | Command | Time | State | Info                  |
+----+------+-----------------+------+---------+------+-------+-----------------------+
|  1 | root | localhost:61513 | test | Query   |    0 | NULL  | show full processlist |
+----+------+-----------------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
</code>
登入後複製
登入後複製
登入後複製
登入後複製

然后执行设置好长连接的脚本并查看数据库会话:

<code>mysql> show full processlist;
+----+------+-----------------+------+---------+------+-------+-----------------------+
| Id | User | Host            | db   | Command | Time | State | Info                  |
+----+------+-----------------+------+---------+------+-------+-----------------------+
|  1 | root | localhost:61513 | test | Query   |    0 | NULL  | show full processlist |
|  5 | test | localhost:61569 | test | Sleep   |    2 |       | NULL                  |
+----+------+-----------------+------+---------+------+-------+-----------------------+
2 rows in set (0.00 sec)
</code>
登入後複製

结论2:apache2handler sapi也是支持db长连接的

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!