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/