pgjdbc中loginTimeout、connectTimeout和socketTimeout有什么区别

What's the difference between loginTimeout, connectTimeout and socketTimeout in pgjdbc

在 pgjdbc 中我们有:

但我不清楚 loginTimeoutconnectTimeoutsocketTimeout.

之间有什么区别(何时应用)

看完源码,我想是这样的:

  • connectTimeout 指定等待 TCP 网络连接建立的时间

  • loginTimeout 指定登录数据库的整个过程允许花费多长时间

  • socketTimeout 指定客户端在抛出错误之前等待服务器响应命令的时间

前两个与建立连接有关,第三个与整个数据库会话有关。

建立 TCP 连接是建立数据库连接的一部分。

PostgreSQL JDBC documentation 中所述:

  • loginTimeout = int

    Specify how long to wait for establishment of a database connection. The timeout is specified in seconds.

  • connectTimeout = int

    The timeout value used for socket connect operations. If connecting to the server takes longer than this value, the connection is broken. The timeout is specified in seconds and a value of zero means that it is disabled.

  • socketTimeout = int

    The timeout value used for socket read operations. If reading from the server takes longer than this value, the connection is closed. This can be used as both a brute force global query timeout and a method of detecting network problems. The timeout is specified in seconds and a value of zero means that it is disabled.

  • cancelSignalTimeout = int

    Cancel command is sent out of band over its own connection, so cancel message can itself get stuck. This property controls "connect timeout" and "socket timeout" used for cancel commands. The timeout is specified in seconds. Default value is 10 seconds.

connectTimeoutsocketTimeout 是 low-level 套接字操作的超时。 connectTimeout 控制建立 TCP 套接字连接所需的时间。建立 TCP 连接并不能保证登录(它甚至不能保证你连接到 PostgreSQL 服务器,只是你连接到接受你的 TCP 连接的东西)。 socketTimeout 控制套接字可以被阻塞等待从套接字读取的时间。这涉及 所有 从服务器读取,不仅在连接期间,而且在与服务器的后续交互期间(例如执行查询)。

另一方面,loginTimeout 管理连接和验证 PostgreSQL 服务器的 PostgreSQL 协议操作。这涉及建立一个 TCP 连接,然后是一个或多个数据包交换,用于与 PostgreSQL 服务器的握手和身份验证(我不熟悉 PostgreSQL 协议的细节,所以我不能说得很具体)。

交换这些数据包可能需要额外的时间,或者如果您连接的不是 PostgreSQL 服务器,则数据包交换可能会停止。仔细控制 connectTimeoutsocketTimeout 可能会解决这个问题,但没有任何保证(例如正在交换数据,但登录永远不会完成)。此外,由于 socketTimeout 还管理连接上的所有其他操作,您可能希望将其设置得比您愿意等待的更高(例如,对于需要很长时间才能获得响应的其他操作)登录完成。

cancelSignalTimeout 用作用于取消命令的单独 TCP 连接的连接 套接字超时。