6

In pgjdbc we have:

  • loginTimeout
  • connectTimeout
  • socketTimeout
  • cancelSignalTimeout

But it isn't clear to me what's the difference (when are they applied) between loginTimeout, connectTimeout and socketTimeout.

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • Have you read the [documentation](https://jdbc.postgresql.org/documentation/head/connect.html#connection-parameters)? If so what is unclear about that? – Mark Rotteveel May 23 '19 at 08:15
  • Yes I have. I can't see how `connectTimeout` is different from `loginTimeout`, as far as I understand, the client will establish a SSL connection with the server and then login at the database... So does the `loginTimeout` starts after the SSL connection is established, or does it start together with the `connectTimeout`, since I submitted a login command that happens to use SSL? And will `socketTimeout` be applied at login time as well, since it's a socket read I guess? – Michel Feinstein May 23 '19 at 13:09

2 Answers2

3

As documented in the 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.

The connectTimeout and socketTimeout are timeouts on low-level socket operations. The connectTimeout governs the time needed to establish a TCP socket connection. Establishing a TCP connection doesn't guarantee a login (it doesn't even guarantee that you're connecting to a PostgreSQL server, just that you connected to something that accepted your TCP connection). A socketTimeout governs the time a socket can be blocked waiting to read from a socket. This involves all reads from the server, not just during connect, but also during subsequent interaction with the server (eg executing queries).

On the other hand loginTimeout governs the PostgreSQL protocol operation of connecting and authenticating to the PostgreSQL server. This involves establishing a TCP connection followed by one or more exchanges of packets for the handshake and authentication to the PostgreSQL server (I'm not familiar with the details of the PostgreSQL protocol, so I can't be very specific).

Exchanging these packets can take additional time, or if you connected to something that isn't a PostgreSQL server the packet exchange may stall. It might be possible to solve this with careful control of both connectTimeout and socketTimeout, but there are no guarantees (eg data is being exchanged, but the login never completes). In addition, as the socketTimeout also governs all other operations on the connection, you may want to set it higher (eg for other operations that take a long time to get a response back) than you are willing to wait for the login to complete.

The cancelSignalTimeout is used as the connect and socket timeout of the separate TCP connection used for cancel commands.

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • So in a sense `loginTimeout` >= `connectTimeout` >= `socketTimeout`, right? – Michel Feinstein May 24 '19 at 13:55
  • @mFeinstein The first two: yes, the last one should be viewed as independent. It could be larger or smaller depending on the effect you want as it is applied to **all** reads from the socket. I'd say you usually want it larger though. If it is smaller than the `loginTimeout`, it is possible that a login is cut short by a read timeout before the login timeout expires (although that will depend on the exact protocol exchange necessary for the login). – Mark Rotteveel May 24 '19 at 14:33
  • Perfect! Thanks Mark! – Michel Feinstein May 24 '19 at 14:50
2

After reading the source, I'd say it is like this:

  • connectTimeout specifies how long to wait for a TCP network connection to get established

  • loginTimeout specifies how long the whole process of logging into the database is allowed to take

  • socketTimeout specifies how long the client will wait for a response to a command from the server before throwing an error

The first two are related to establishing a connection, the third is relevant for the whole database session.

Establishing a TCP connection is part of establishing a database connection.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263