【问题标题】:PostgreSQL frontend unexpectedly closes connectionPostgreSQL 前端意外关闭连接
【发布时间】:2014-01-22 11:48:53
【问题描述】:

我对以下情况有点困惑。

我在主机 A 上运行了一个 Postgres 服务器,在主机 B 上运行了一个基于 java 的客户端。客户端使用 org.postgresql.Driver JDBC 驱动程序(版本 9.1-901.jdbc3)。

有时在执行长时间运行的存储过程时出现异常“java.net.SocketException: Socket closed”。我正在使用org.apache.commons.dbcp.BasicDataSource 进行检索 连接。

DBCP 池配置有默认选项。

我得到了 tcp 转储,以便确定正在关闭哪一侧(客户端或服务器)的套接字;

这是我得到的:

1.   Client B sends a test query message when tries to borrow connection from dbcp pool ("Select 1")
2.   Server A sends successful response back (Type: Command completion, Ready for query)
3.   Client B sends ACK message in response on server A response (see the item 2).
4.   Client B sends query message to the server A.
5.   Server A sends ACK message in response on client Query message (see the item 4).
6.   Client B sends terminating message (Type : Termination) after some time passed (from 3 to 10 or sometimes even more minutes).
7    Client B sends FIN ACK message to the server.
8.   Server A sends back ACK on termination message.
9.   Server A sends ACK on (FIN, ACK) message (item 7).
10.  Server A sends back a response on the client query (from item 4) Type: Row description Columns: 40.
11.  Client B sends RST message (reset).
12.  Server A continues sending response on the query Type: Data row Length: 438 Columns 40 and so on.
13   Client B sends RST message (reset) again.
14.  Server A continues sending response on the query Type: Data row Length: 438 Columns 40 and so on.
15.  Client B sends RST message (reset).

在那之后,通信似乎结束了。

在第 6 项之后,在我的客户端日志中出现如下异常:

Caused by: java.net.SocketException: Socket closed
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:152)
        at java.net.SocketInputStream.read(SocketInputStream.java:122)
        at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:145)
        at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:114)
        at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:73)
        at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:274)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1661)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)

请您帮我找出这种失败的原因。 (此错误每 10 个成功案例发生一次。)

【问题讨论】:

  • PostgreSQL 错误日志中会出现什么(如果有)?考虑使用log_min_messages = debug3 重新启动 PostgreSQL,以获得详细、详细(和巨大)的日志。你需要一个合适的log_line_prefix,这样你就可以知道哪个会话是哪个会话。还有,PostgreSQL 版本? (SELECT version())。最后,客户端和服务器之间的网络是什么样的?多少跳?互联网,还是只有局域网?涉及WiFi?任何 NAT、防火墙、代理、连接跟踪路由器?

标签: postgresql jdbc apache-commons-dbcp


【解决方案1】:

我们遇到了类似的问题,它是由服务器和客户端之间的防火墙或连接跟踪路由器引起的。

我猜你在服务器端使用了 tcpdump。查询运行了相当长的时间,连接上没有流量。防火墙在打开的连接上有一个计时器;它过期了,防火墙关闭了与服务器的连接,也关闭了与客户端的连接。在服务器端的捕获中,客户端似乎正在关闭连接。

您可以通过在服务器端捕获的同时在客户端捕获来验证这一点 - 在客户端,它看起来像服务器已经关闭了连接,而在服务器端,它看起来像服务器正在关闭联系。实际上,防火墙正在双向关闭它。

为防止这种情况,您可以设置tcp_keepalives_idletcp_keepalives_interval 和/或tcp_keepalives_count(如果您的操作系统支持 TCP Keepalives)。或者,您必须更改防火墙上的设置。

【讨论】:

    猜你喜欢
    • 2012-01-25
    • 2020-12-23
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多