php持久化连接数据库

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

PDO::ATTR_PERSISTENT => true

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

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

php测试代码:

<?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());

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

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)

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

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)

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

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)

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

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

mysql> kill 3;
Query OK, 0 rows affected (0.00 sec)

查看数据库会话列表:

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)

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

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)

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

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)

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2536
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2162
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

31710
来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

2968
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2645
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

2070
来自专栏魂祭心

原 canvas绘制clock

4084
来自专栏落花落雨不落叶

canvas画简单电路图

62011
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3145
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

5476

扫码关注云+社区