php - TCP Keep-Alive PDO 连接参数

标签 php postgresql pdo tcp keep-alive

PHP 的 PDO(或它各自的 PostgreSQL driver)是否有启用 TCP keep-alive 探测的连接配置选项 like JDBC

我遇到了一个问题,我们通过 NAT 建立连接,5 分钟后连接断开(我无法更改),而我们在外部 Postgres 实例上运行的查询需要超过 5 分钟分钟运行,导致我们的客户端永远不会收到来自 Postgres 实例的响应并最终超时。

最佳答案

PDO PostgreSQL 驱动程序建立在 libpq 客户端库之上。驱动程序允许以键/值对的形式在 DSN 中传递特定的 libpq 连接选项,其中包括 TCP keepalives 选项。

来自 PostgreSQL doc :

keepalives

Controls whether client-side TCP keepalives are used. The default value is 1, meaning on, but you can change this to 0, meaning off, if keepalives are not wanted. This parameter is ignored for connections made via a Unix-domain socket.

keepalives_idle

Controls the number of seconds of inactivity after which TCP should send a keepalive message to the server. A value of zero uses the system default. This parameter is ignored for connections made via a Unix-domain socket, or if keepalives are disabled. It is only supported on systems where the TCP_KEEPIDLE or TCP_KEEPALIVE socket option is available, and on Windows; on other systems, it has no effect.

keepalives_interval

Controls the number of seconds after which a TCP keepalive message that is not acknowledged by the server should be retransmitted. A value of zero uses the system default. This parameter is ignored for connections made via a Unix-domain socket, or if keepalives are disabled. It is only supported on systems where the TCP_KEEPINTVL socket option is available, and on Windows; on other systems, it has no effect.

例子:

<?
$db = new PDO('pgsql:dbname=mydb;host=localhost;user=myuser;password=mypass;keepalives_idle=60');
?>

关于php - TCP Keep-Alive PDO 连接参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39962666/

相关文章:

PHP PDO 获取对象

php - 使用 PDO 实例执行查询时出现段错误

PHP preg_replace_callback,仅替换 1 个反向引用?

PHP 数组合并并删除重复项

c - Postgresql C 函数,不加载外部 dll 文件

ruby-on-rails - Ruby on rails - 参数数量错误(2 为 1)[Rails]

javascript - sails.js 中使用不同数据类型的“或”查询

php - 警告:PDOStatement::bindValue():SQLSTATE[HY093]:参数编号无效:列/参数基于 1

php - 为什么 Symfony2 在登录时替换 PHPSESSID?

php - laravel 路由在 linux 中不工作,但在 unix 中工作