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.本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
- ¥15 电脑蓝屏logfilessrtsrttrail问题
- ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
- ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
- ¥15 Java+vue部署版本反编译
- ¥100 对反编译和ai熟悉的开发者。
- ¥15 带序列特征的多输出预测模型
- ¥15 Python 如何安装 distutils模块
- ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
- ¥15 资源泄露软件闪退怎么解决?