Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for jdk.net.ExtendedSocketOptions and ignore errors #2043

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
review updates
  • Loading branch information
lilgreenbird committed Jan 18, 2023
commit 440bfdb2c5167d5163a19a83f23726c23a651d5c
35 changes: 19 additions & 16 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,13 @@ final class TDSChannel implements Serializable {

private static final Logger logger = Logger.getLogger("com.microsoft.sqlserver.jdbc.internals.TDS.Channel");

/**
* From jdk.net.ExtendedSocketOption for setting TCP keep-alive options
*/
private static Method socketSetOptionMethod = null;
private static SocketOption<Integer> socketKeepIdleOption = null;
private static SocketOption<Integer> socketKeepIntervalOption = null;

final Logger getLogger() {
return logger;
}
Expand Down Expand Up @@ -734,29 +741,25 @@ final InetSocketAddress open(String host, int port, int timeoutMillis, boolean u
return (InetSocketAddress) channelSocket.getRemoteSocketAddress();
}

/**
* Set TCP keep-alive options for idle connection resiliency
*/
@SuppressWarnings("unchecked")
private void setSocketOptions(Socket tcpSocket, TDSChannel channel) throws IOException {
Method setOptionMethod = null;
SocketOption<Integer> keepIdleOption = null;
SocketOption<Integer> keepIntervalOption = null;

try {
setOptionMethod = Socket.class.getMethod("setOption", SocketOption.class, Object.class);
Class<?> clazz = Class.forName("jdk.net.ExtendedSocketOptions");
keepIdleOption = (SocketOption<Integer>) clazz.getDeclaredField("TCP_KEEPIDLE").get(null);
keepIntervalOption = (SocketOption<Integer>) clazz.getDeclaredField("TCP_KEEPINTERVAL").get(null);

if (setOptionMethod != null && keepIdleOption != null && keepIntervalOption != null) {
if (socketSetOptionMethod == null) {
socketSetOptionMethod = Socket.class.getMethod("setOption", SocketOption.class, Object.class);
Class<?> clazz = Class.forName("jdk.net.ExtendedSocketOptions");
socketKeepIdleOption = (SocketOption<Integer>) clazz.getDeclaredField("TCP_KEEPIDLE").get(null);
socketKeepIntervalOption = (SocketOption<Integer>) clazz.getDeclaredField("TCP_KEEPINTERVAL").get(null);
} else {
if (logger.isLoggable(Level.FINER)) {
logger.finer(channel.toString() + ": Setting KeepAlive extended socket options.");
}

setOptionMethod.invoke(tcpSocket, keepIdleOption, 30); // 30 seconds
setOptionMethod.invoke(tcpSocket, keepIntervalOption, 1); // 1 second
socketSetOptionMethod.invoke(tcpSocket, socketKeepIdleOption, 30); // 30 seconds
socketSetOptionMethod.invoke(tcpSocket, socketKeepIntervalOption, 1); // 1 second

} else if (logger.isLoggable(Level.FINER)) {
logger.finer(
channel.toString() + ": KeepAlive extended socket options not supported on this platform.");
}
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | IllegalAccessException
| InvocationTargetException e) {
Expand All @@ -766,7 +769,7 @@ private void setSocketOptions(Socket tcpSocket, TDSChannel channel) throws IOExc
}
}
}

/**
* Disables SSL on this TDS channel.
*/
Expand Down