I have an Apache/PHP app on one server and a PostgreSQL database on a second server. They are configured to connect via SSL. Everything works, except that even though $db->setAttribute(PDO::ATTR_PERSISTENT, true);
is in the script the TCP connections are always closed. PHP.INI has pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = 30
and pgsql.max_links = 30
. Using sudo netstat -nap
I can see that there are no ESTABLISHED connections between the two servers. What else do I need to do to keep the connections open? New connection overhead is 10-12 ms per request.
php postgresql持久连接总是关闭
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- dqu94359 2016-07-06 18:05关注
I found this note a fair way down one of the php manual pages:
If you wish to use persistent connections, you must set PDO::ATTR_PERSISTENT in the array of driver options passed to the PDO constructor. If setting this attribute with PDO::setAttribute() after instantiation of the object, the driver will not use persistent connections.
That's where I went wrong. I was using this:
$host = explode('.',$_SERVER['SERVER_NAME']); $db = new PDO($host[0]); $db->setAttribute(PDO::ATTR_PERSISTENT, true); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
When I should have been doing this:
$host = explode('.', $_SERVER['SERVER_NAME']); $db = new PDO($host[0],null,null,array(PDO::ATTR_PERSISTENT=>true, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC));
The DSN's are in a pdo.ini file. The null user and password are fortunately ignored or else this wouldn't work either. Now
new PDO
takes 0.02 ms instead of 10 ms.本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 用雷电模拟器安装百达屋apk一直闪退
- ¥15 算能科技20240506咨询(拒绝大模型回答)
- ¥15 自适应 AR 模型 参数估计Matlab程序
- ¥100 角动量包络面如何用MATLAB绘制
- ¥15 merge函数占用内存过大
- ¥15 Revit2020下载问题
- ¥15 使用EMD去噪处理RML2016数据集时候的原理
- ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
- ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
- ¥15 Oracle中如何从clob类型截取特定字符串后面的字符