TCP Keep-Alive PDO Connection Parameter

徘徊边缘 提交于 2019-12-11 05:28:26

问题


Does PHP's PDO (or it's respective PostgreSQL driver) have a connection configuration option for enabling a TCP keep-alive probe like JDBC?

I'm experiencing an issue where we're making a connection through a NAT which drops connections after 5 minutes (I can't change this), and the query we're running on an external Postgres instance takes longer than 5 minutes to run, causing our client to never receive a response from the Postgres instance and eventually time out.


回答1:


The PDO PostgreSQL driver is built on top of the libpq client library. The driver allows to pass specific libpq connections options in the DSN in the form of key/values pairs, among which the TCP keepalives options.

From 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.

Example:

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



回答2:


You can update the kernel keepalive time:

echo 250 > /proc/sys/net/ipv4/tcp_keepalive_time

(credit Chris Merrick)



来源:https://stackoverflow.com/questions/39962666/tcp-keep-alive-pdo-connection-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!