在让数据库连接空闲一段时间后,我的应用程序出现以下异常:
... An I/O error occured while sending to the backend.; nested exception is org.postgresql.util.PSQLException: An I/O error occured while sending to the backend.] with root cause
java.net.SocketException: Operation timed out
at java.net.SocketInputStream.socketRead0(Native Method)
同样的问题发生在 psql 中,而且我在连接到本地数据库时没有问题,所以我很确定问题出在 RDS 上。
psql=> select 'ok';
SSL SYSCALL error: Operation timed out
psql=> select 'ok';
SSL SYSCALL error: EOF detected
The connection to the server was lost. Attempting reset: Succeeded.
我找到了这个 other question这表明可以解决问题(超时现在需要更长的时间)但没有解决问题。
我将 Spring Boot 与 JDBC(tomcat 连接池)和 JDBCTemplate 结合使用。
是否有解决方法或修复方法? 也许强制连接池进行测试和重新连接? 我该如何在这种环境中做到这一点?
编辑: 这是我的连接字符串
jdbc:postgresql://myhost.c2estvxozjm3.eu-west-1.rds.amazonaws.com/dashboard?tcpKeepAlive=true
解决方案:
按照所选答案中的建议编辑 RDS 服务器端 TCP_KeepAlive 参数。我使用的参数是:
tcp_keepalives_count 5
tcp_keepalives_idle 200
tcp_keepalives_interval 200
最佳答案
它看起来像某些东西 - 可能是您端的 NAT 路由器,可能是 AWS 端的某些东西 - 是连接跟踪,并且在一段时间后忘记了连接。
我建议 enabling TCP keepalives .您可以在 AWS RDS 配置中启用它们的服务器端;如果没有,您可以在 JDBC 驱动程序的客户端请求它们。
TCP keepalive 比验证/测试查询要好得多,因为它们的开销要低得多,而且它们不会在服务器查询日志中产生不必要的垃圾日志。
关于postgresql - 从 Azure 连接到 Amazon RDS 上的 Postgres 数据库时超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22696745/