add socketTimeoutInConnection parameters

This commit is contained in:
justbk
2022-08-23 11:10:05 +08:00
committed by justbk
parent 996c7b7b59
commit 70af2f4220
2 changed files with 23 additions and 9 deletions

View File

@ -283,6 +283,14 @@ public enum PGProperty {
*/
SOCKET_TIMEOUT("socketTimeout", "0", "The timeout value used for socket read operations."),
/**
* The timeout value used for socket read operations when jdbc connecting. 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.
*/
SOCKET_TIMEOUT_IN_CONNECTING("socketTimeoutInConnecting", "5", "The timeout value used for socket read operations when jdbc connecting."),
/**
* Cancel command is sent out of band over its own connection, so cancel message can itself get
* stuck.

View File

@ -98,6 +98,19 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
}
private void setSocketTimeout(PGStream stream, Properties info, PGProperty propKey) throws SQLException, IOException {
// Set the socket timeout if the "socketTimeout" property has been set.
int socketTimeout = Integer.parseInt(propKey.getDefaultValue());
if (propKey.getInt(info) <= Integer.MAX_VALUE / 1000) {
socketTimeout = propKey.getInt(info);
} else {
LOGGER.debug("integer socketTimeout is too large, it will occur error after multiply by 1000.");
}
if (socketTimeout >= 0) {
stream.getSocket().setSoTimeout(socketTimeout * 1000);
}
}
private PGStream tryConnect(String user, String database,
Properties info, SocketFactory socketFactory, HostSpec hostSpec,
SslMode sslMode)
@ -115,15 +128,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
newStream = enableSSL(newStream, sslMode, info, connectTimeout);
// Set the socket timeout if the "socketTimeout" property has been set.
int socketTimeout = Integer.parseInt(PGProperty.SOCKET_TIMEOUT.getDefaultValue());
if (PGProperty.SOCKET_TIMEOUT.getInt(info) <= Integer.MAX_VALUE / 1000) {
socketTimeout = PGProperty.SOCKET_TIMEOUT.getInt(info);
} else {
LOGGER.debug("integer socketTimeout is too large, it will occur error after multiply by 1000.");
}
if (socketTimeout > 0) {
newStream.getSocket().setSoTimeout(socketTimeout * 1000);
}
setSocketTimeout(newStream, info, PGProperty.SOCKET_TIMEOUT_IN_CONNECTING);
// Enable TCP keep-alive probe if required.
boolean requireTCPKeepAlive = PGProperty.TCP_KEEP_ALIVE.getBoolean(info);
@ -162,6 +167,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
// Do authentication (until AuthenticationOk).
doAuthentication(newStream, hostSpec.getHost(), user, info);
setSocketTimeout(newStream, info, PGProperty.SOCKET_TIMEOUT);
return newStream;
}