[英]PHP Keeping a PDO ATTR_PERSISTENT connection alive
I am running an endless PHP Script with PDO Persistent Connection like this: 我正在使用PDO持久连接运行无尽的PHP脚本,如下所示:
$conn=new PDO(
'mysql:host=127.0.0.1','user','pass', array(PDO::ATTR_PERSISTENT => true)
);
The mySQL wait_timeout
variable is set to 28800
and I left the script on idle for 12 hours for test purposes; mySQL
wait_timeout
变量设置为28800
,出于测试目的,我将该脚本保持空闲状态12个小时。 and the connection is dropped automatically so I presume the PDO::ATTR_PERSISTENT
attribute is superseded by the system variable wait_timeout
. 并且连接自动断开,因此我假定
PDO::ATTR_PERSISTENT
属性被系统变量wait_timeout
取代。
So I was wondering whether theres a setting or another PHP method to keep the connection breathing as long as the PHP script is running and whether or not that would be a good practice. 因此,我想知道是否有设置或另一种PHP方法可以在PHP脚本运行时保持连接畅通,这是否是一个好习惯。
My plan B would be executing a resource-less mySQL query every 60 minutes to reset the clock. 我的计划B将每60分钟执行一次无资源的mySQL查询以重置时钟。
OS: 4GB RAM VPS Debian 64 Bit SSD 操作系统:4GB RAM VPS Debian 64 Bit SSD
I would suggest you another way to handle such situations. 我建议您使用另一种方法来处理这种情况。 Whenever you need to run a query in your long-lasting script ensure that there is a connnection.
每当您需要在持久脚本中运行查询时,请确保存在连接。 Otherwise reconnect.
否则,请重新连接。
try {
echo "Testing connection...\n";
$old_errlevel = error_reporting(0);
self::$pdo->query("SELECT 1");
} catch (PDOException $e) {
echo "Connection failed, reinitializing...\n";
self::init();
}
You can find the full class example here . 您可以在此处找到完整的类示例 。 I also suggest you to explicitly close the connection in your script when you know you will not use it for a long period.
我还建议您在知道将长时间不使用它时在脚本中明确关闭该连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.