Skip to content

Commit

Permalink
resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird committed Jan 19, 2023
2 parents 6bf40f5 + b4aa61e commit 68a2f46
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 54 deletions.
41 changes: 40 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.io.Reader;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
Expand All @@ -25,6 +27,7 @@
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketOption;
import java.net.SocketTimeoutException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -600,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 @@ -717,7 +727,7 @@ final InetSocketAddress open(String host, int port, int timeoutMillis, boolean u
// Set socket options
tcpSocket.setTcpNoDelay(true);
tcpSocket.setKeepAlive(true);
DriverJDBCVersion.setSocketOptions(tcpSocket, this);
setSocketOptions(tcpSocket, this);

// set SO_TIMEOUT
int socketTimeout = con.getSocketTimeoutMilliseconds();
Expand All @@ -731,6 +741,35 @@ 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 {
try {
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.");
}

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

}
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | IllegalAccessException
| InvocationTargetException e) {
if (logger.isLoggable(Level.FINER)) {
logger.finer(
channel.toString() + ": KeepAlive extended socket options not supported on this platform.");
}
}
}

/**
* Disables SSL on this TDS channel.
*/
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

package com.microsoft.sqlserver.jdbc;

import java.net.Socket;
import java.sql.BatchUpdateException;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
Expand All @@ -28,8 +25,6 @@ private DriverJDBCVersion() {
}


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

static final boolean checkSupportsJDBC43() {
return false;
}
Expand All @@ -50,11 +45,4 @@ static SQLServerConnection getSQLServerConnection(String parentInfo) throws SQLS
static int getProcessId() {
return pid;
}

static void setSocketOptions(Socket tcpSocket, TDSChannel channel) {
if (logger.isLoggable(Level.FINER)) {
logger.finer(
"Socket.supportedOptions() not available on this JVM. Extended KeepAlive options will not be set.");
}
}
}
24 changes: 0 additions & 24 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@

package com.microsoft.sqlserver.jdbc;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketOption;
import java.sql.BatchUpdateException;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import jdk.net.ExtendedSocketOptions;


/**
Expand All @@ -32,8 +24,6 @@ private DriverJDBCVersion() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}

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

static final boolean checkSupportsJDBC43() {
return true;
}
Expand Down Expand Up @@ -65,18 +55,4 @@ static SQLServerConnection getSQLServerConnection(String parentInfo) throws SQLS
static int getProcessId() {
return pid;
}

static void setSocketOptions(Socket tcpSocket, TDSChannel channel) throws IOException {
Set<SocketOption<?>> options = tcpSocket.supportedOptions();
if (options.contains(ExtendedSocketOptions.TCP_KEEPIDLE)
&& options.contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
if (logger.isLoggable(Level.FINER)) {
logger.finer(channel.toString() + ": Setting KeepAlive extended socket options.");
}
tcpSocket.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, 30); // 30 seconds
tcpSocket.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, 1); // 1 second
} else if (logger.isLoggable(Level.FINER)) {
logger.finer(channel.toString() + ": KeepAlive extended socket options not supported on this platform.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.sql.SQLException;
import java.util.Properties;

import com.microsoft.sqlserver.jdbc.TestUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -227,10 +226,11 @@ public void testADPasswordUnregisteredUserWithConnectionStringUserName() throws
fail(EXPECTED_EXCEPTION_NOT_THROWN);
} catch (SQLServerException e) {
assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(),
e.getMessage()
(e.getMessage()
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD));
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD))
|| e.getMessage().contains(ERR_MSG_REQUEST_THROTTLED));
}
}

Expand All @@ -248,10 +248,11 @@ public void testADPasswordUnregisteredUserWithDatasource() throws SQLException {
fail(EXPECTED_EXCEPTION_NOT_THROWN);
} catch (SQLServerException e) {
assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(),
e.getMessage()
(e.getMessage()
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD));
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD))
|| e.getMessage().contains(ERR_MSG_REQUEST_THROTTLED));
}
}

Expand All @@ -262,10 +263,11 @@ public void testADPasswordUnregisteredUserWithConnectionStringUser() throws SQLE
fail(EXPECTED_EXCEPTION_NOT_THROWN);
} catch (SQLServerException e) {
assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(),
e.getMessage()
(e.getMessage()
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD));
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD))
|| e.getMessage().contains(ERR_MSG_REQUEST_THROTTLED));
}
}

Expand Down Expand Up @@ -398,11 +400,12 @@ public void testADPasswordWrongPasswordWithConnectionStringUserName() throws SQL
fail(EXPECTED_EXCEPTION_NOT_THROWN);
}

assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(), e.getMessage()
assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(), (e.getMessage()
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + azureUserName
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
&& (e.getCause().getCause().getMessage().toLowerCase().contains("invalid username or password")
|| e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_TOO_MANY)));
&& e.getCause().getCause().getMessage().toLowerCase().contains("invalid username or password")
|| e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_TOO_MANY))
|| e.getMessage().contains(ERR_MSG_REQUEST_THROTTLED));
}
}

Expand All @@ -423,11 +426,12 @@ public void testADPasswordWrongPasswordWithDatasource() throws SQLException {
fail(EXPECTED_EXCEPTION_NOT_THROWN);
}

assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(), e.getMessage()
assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(), (e.getMessage()
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + azureUserName
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
&& (e.getCause().getCause().getMessage().toLowerCase().contains("invalid username or password")
|| e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_TOO_MANY)));
&& e.getCause().getCause().getMessage().toLowerCase().contains("invalid username or password")
|| e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_TOO_MANY))
|| e.getMessage().contains(ERR_MSG_REQUEST_THROTTLED));
}
}

Expand All @@ -442,11 +446,12 @@ public void testADPasswordWrongPasswordWithConnectionStringUser() throws SQLExce
fail(EXPECTED_EXCEPTION_NOT_THROWN);
}

assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(), e.getMessage()
assertTrue(INVALID_EXCEPTION_MSG + ": " + e.getMessage(), (e.getMessage()
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + azureUserName
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
&& (e.getCause().getCause().getMessage().toLowerCase().contains("invalid username or password")
|| e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_TOO_MANY)));
&& e.getCause().getCause().getMessage().toLowerCase().contains("invalid username or password")
|| e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_TOO_MANY))
|| e.getMessage().contains(ERR_MSG_REQUEST_THROTTLED));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class FedauthCommon extends AbstractTest {
static final String ERR_MSG_RESULTSET_IS_CLOSED = TestUtils.R_BUNDLE.getString("R_resultsetClosed");
static final String ERR_MSG_SOCKET_CLOSED = TestResource.getResource("R_socketClosed");
static final String ERR_TCPIP_CONNECTION = TestResource.getResource("R_tcpipConnectionToHost");
static final String ERR_MSG_REQUEST_THROTTLED = "Request was throttled";

enum SqlAuthentication {
NotSpecified,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void testBasicConnectionAAD() throws SQLException {
org.junit.Assume.assumeTrue(azureServer != null && !azureServer.isEmpty());

basicReconnect("jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";user=" + azureUserName
+ ";password=" + azurePassword + ";loginTimeout=30;Authentication=ActiveDirectoryPassword");
+ ";password=" + azurePassword + ";loginTimeout=90;Authentication=ActiveDirectoryPassword");
}

@Test
Expand Down

0 comments on commit 68a2f46

Please sign in to comment.