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

@ -282,6 +282,14 @@ public enum PGProperty {
* value of zero means that it is disabled. * value of zero means that it is disabled.
*/ */
SOCKET_TIMEOUT("socketTimeout", "0", "The timeout value used for socket read operations."), 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 * Cancel command is sent out of band over its own connection, so cancel message can itself get

View File

@ -97,6 +97,19 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
setStaticUseBoolean(useBoolean); setStaticUseBoolean(useBoolean);
} }
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, private PGStream tryConnect(String user, String database,
Properties info, SocketFactory socketFactory, HostSpec hostSpec, Properties info, SocketFactory socketFactory, HostSpec hostSpec,
@ -115,15 +128,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
newStream = enableSSL(newStream, sslMode, info, connectTimeout); newStream = enableSSL(newStream, sslMode, info, connectTimeout);
// Set the socket timeout if the "socketTimeout" property has been set. // Set the socket timeout if the "socketTimeout" property has been set.
int socketTimeout = Integer.parseInt(PGProperty.SOCKET_TIMEOUT.getDefaultValue()); setSocketTimeout(newStream, info, PGProperty.SOCKET_TIMEOUT_IN_CONNECTING);
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);
}
// Enable TCP keep-alive probe if required. // Enable TCP keep-alive probe if required.
boolean requireTCPKeepAlive = PGProperty.TCP_KEEP_ALIVE.getBoolean(info); boolean requireTCPKeepAlive = PGProperty.TCP_KEEP_ALIVE.getBoolean(info);
@ -162,6 +167,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
// Do authentication (until AuthenticationOk). // Do authentication (until AuthenticationOk).
doAuthentication(newStream, hostSpec.getHost(), user, info); doAuthentication(newStream, hostSpec.getHost(), user, info);
setSocketTimeout(newStream, info, PGProperty.SOCKET_TIMEOUT);
return newStream; return newStream;
} }