From 8a65c61b9df1f9162f386e39bd481fe948792f37 Mon Sep 17 00:00:00 2001 From: David Engel Date: Thu, 29 Jul 2021 14:30:12 -0700 Subject: [PATCH 1/4] Remove Java 9-specific class references from the Java 8 jar --- pom.xml | 1 + .../sqlserver/jdbc/SQLServerConnection.java | 17 +-------------- .../sqlserver/jdbc/SQLServerDataSource.java | 6 +----- .../sqlserver/jdbc/SQLServerDriver.java | 6 +----- .../sqlserver/jdbc/SQLServerJdbc42.java | 11 ++++++++++ .../sqlserver/jdbc/SQLServerJdbc43.java | 20 ++++++++++++++++++ .../sqlserver/jdbc/SQLServerXAConnection.java | 6 +----- .../com/microsoft/sqlserver/jdbc/Util.java | 21 ------------------- 8 files changed, 36 insertions(+), 52 deletions(-) diff --git a/pom.xml b/pom.xml index fe4c4ccb7e..89711e4b2f 100644 --- a/pom.xml +++ b/pom.xml @@ -252,6 +252,7 @@ **/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java + **/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java 1.8 1.8 diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index da4731e7ae..e7194af6b8 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -1216,21 +1216,6 @@ final SQLCollation getDatabaseCollation() { .getLogger("com.microsoft.sqlserver.jdbc.Connection"); private static String loggingClassName = "com.microsoft.sqlserver.jdbc.SQLServerConnection:"; - /** Client process ID sent during login */ - private static int pid = 0; - - static { - long pidLong = 0; - try { - pidLong = ProcessHandle.current().pid(); - } catch (NoClassDefFoundError e) { // ProcessHandle is Java 9+ - if (loggerExternal.isLoggable(Level.FINER) && Util.isActivityTraceOn()) { - loggerExternal.finer(loggingClassName + " NoClassDefFoundError for ProcessHandle. ProcessId will be 0."); - } - } - pid = (pidLong > Integer.MAX_VALUE) ? 0 : (int)pidLong; - } - /** * There are three ways to get a failover partner connection string, from the failover map, the connecting server * returned the following variable only stores the serverReturned failver information. @@ -5379,7 +5364,7 @@ final boolean complete(LogonCommand logonCommand, TDSReader tdsReader) throws SQ tdsWriter.writeInt(tdsVersion); tdsWriter.writeInt(requestedPacketSize); tdsWriter.writeBytes(interfaceLibVersionBytes); // writeBytes() is little endian - tdsWriter.writeInt(pid); // Client process ID + tdsWriter.writeInt(DriverJDBCVersion.getProcessId()); // Client process ID tdsWriter.writeInt(0); // Primary server connection ID tdsWriter.writeByte((byte) (// OptionFlags1: diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java index 26d96805f8..67297f7492 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDataSource.java @@ -1313,11 +1313,7 @@ SQLServerConnection getConnectionInternal(String username, String password, if (dsLogger.isLoggable(Level.FINER)) dsLogger.finer(toString() + " Begin create new connection."); SQLServerConnection result = null; - if (Util.use43Wrapper()) { - result = new SQLServerConnection43(toString()); - } else { - result = new SQLServerConnection(toString()); - } + result = DriverJDBCVersion.getSQLServerConnection(toString()); result.connect(mergedProps, pooledConnection); if (dsLogger.isLoggable(Level.FINER)) dsLogger.finer(toString() + " End create new connection " + result.toString()); diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java index 34879c77df..4848e657ef 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java @@ -911,11 +911,7 @@ public java.sql.Connection connect(String Url, Properties suppliedProperties) th // Merge connectProperties (from URL) and supplied properties from user. Properties connectProperties = parseAndMergeProperties(Url, suppliedProperties); if (connectProperties != null) { - if (Util.use43Wrapper()) { - result = new SQLServerConnection43(toString()); - } else { - result = new SQLServerConnection(toString()); - } + result = DriverJDBCVersion.getSQLServerConnection(toString()); result.connect(connectProperties, null); } loggerExternal.exiting(getClassNameLogging(), "connect", result); diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java index c37a16729f..aad3903775 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java @@ -31,4 +31,15 @@ static final void throwBatchUpdateException(SQLServerException lastError, throw new BatchUpdateException(lastError.getMessage(), lastError.getSQLState(), lastError.getErrorCode(), updateCounts, new Throwable(lastError.getMessage())); } + + static SQLServerConnection getSQLServerConnection(String parentInfo) throws SQLServerException { + return new SQLServerConnection(parentInfo); + } + + /** Client process ID sent during login */ + private static int pid = 0; + + static int getProcessId() { + return pid; + } } \ No newline at end of file diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java index 9279df2ab2..2abf60507b 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java @@ -29,4 +29,24 @@ static final void throwBatchUpdateException(SQLServerException lastError, throw new BatchUpdateException(lastError.getMessage(), lastError.getSQLState(), lastError.getErrorCode(), updateCounts, new Throwable(lastError.getMessage())); } + + static SQLServerConnection getSQLServerConnection(String parentInfo) throws SQLServerException { + return jvmVersion >= 9 ? new SQLServerConnection43(parentInfo) : new SQLServerConnection(parentInfo); + } + + private static double jvmVersion = Double.parseDouble(Util.SYSTEM_SPEC_VERSION); + + /** Client process ID sent during login */ + private static int pid = 0; + + static { + if (jvmVersion >= 9) { + long pidLong = ProcessHandle.current().pid(); + pid = (pidLong > Integer.MAX_VALUE) ? 0 : (int) pidLong; + } + } + + static int getProcessId() { + return pid; + } } diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java index 01268e335f..5f356fc642 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAConnection.java @@ -90,11 +90,7 @@ public final class SQLServerXAConnection extends SQLServerPooledConnection imple if (xaLogger.isLoggable(Level.FINER)) xaLogger.finer("Creating an internal control connection for" + toString()); physicalControlConnection = null; - if (Util.use43Wrapper()) { - physicalControlConnection = new SQLServerConnection43(toString()); - } else { - physicalControlConnection = new SQLServerConnection(toString()); - } + physicalControlConnection = DriverJDBCVersion.getSQLServerConnection(toString()); physicalControlConnection.connect(controlConnectionProperties, null); if (xaLogger.isLoggable(Level.FINER)) xaLogger.finer("Created an internal control connection" + physicalControlConnection.toString() + " for " diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/Util.java b/src/main/java/com/microsoft/sqlserver/jdbc/Util.java index 5182fc2a41..9c97bd1b9f 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/Util.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/Util.java @@ -967,27 +967,6 @@ static synchronized boolean checkIfNeedNewAccessToken(SQLServerConnection connec return false; } - static final boolean use43Wrapper; - - static { - boolean supportJDBC43 = true; - try { - DriverJDBCVersion.checkSupportsJDBC43(); - } catch (UnsupportedOperationException e) { - supportJDBC43 = false; - } - - double jvmVersion = Double.parseDouble(Util.SYSTEM_SPEC_VERSION); - - use43Wrapper = supportJDBC43 && (9 <= jvmVersion); - } - - // if driver is for JDBC 43 and jvm version is 9 or higher, then always return as SQLServerConnection43, - // otherwise return SQLServerConnection - static boolean use43Wrapper() { - return use43Wrapper; - } - @SuppressWarnings("unchecked") static T newInstance(Class returnType, String className, String constructorArg, Object[] msgArgs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { From a5af9494aa13fcc2998b8534cb969d4638ca4b89 Mon Sep 17 00:00:00 2001 From: David Engel Date: Thu, 29 Jul 2021 14:56:23 -0700 Subject: [PATCH 2/4] Exclude JDBC 43 tests from Java 8 build --- pom.xml | 4 ++++ .../microsoft/sqlserver/jdbc/SQLServerJdbc43.java | 12 +++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 89711e4b2f..cf2341605c 100644 --- a/pom.xml +++ b/pom.xml @@ -254,6 +254,10 @@ **/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java **/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java + + **/com/microsoft/sqlserver/jdbc/connection/ConnectionWrapper43Test.java + **/com/microsoft/sqlserver/jdbc/JDBC43Test.java + 1.8 1.8 diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java index 2abf60507b..645e672936 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java @@ -30,20 +30,22 @@ static final void throwBatchUpdateException(SQLServerException lastError, updateCounts, new Throwable(lastError.getMessage())); } + private static double jvmVersion = Double.parseDouble(Util.SYSTEM_SPEC_VERSION); + static SQLServerConnection getSQLServerConnection(String parentInfo) throws SQLServerException { return jvmVersion >= 9 ? new SQLServerConnection43(parentInfo) : new SQLServerConnection(parentInfo); } - private static double jvmVersion = Double.parseDouble(Util.SYSTEM_SPEC_VERSION); - /** Client process ID sent during login */ private static int pid = 0; static { - if (jvmVersion >= 9) { - long pidLong = ProcessHandle.current().pid(); - pid = (pidLong > Integer.MAX_VALUE) ? 0 : (int) pidLong; + long pidLong = 0; + try { + pidLong = ProcessHandle.current().pid(); + } catch (NoClassDefFoundError e) { // ProcessHandle is Java 9+ } + pid = (pidLong > Integer.MAX_VALUE) ? 0 : (int) pidLong; } static int getProcessId() { From ffb626dc8ca09a08b4f5b03b665b64bf0d8a1c7f Mon Sep 17 00:00:00 2001 From: David Engel Date: Thu, 29 Jul 2021 15:27:54 -0700 Subject: [PATCH 3/4] Exclude testClientProcessId against JDBC42 since the tests can run under a newer JVM but the expected result is dependent on what Java version the jar was built with. --- .../sqlserver/jdbc/connection/ClientProcessIdTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/ClientProcessIdTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/ClientProcessIdTest.java index 1428ab9903..f646947821 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/ClientProcessIdTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/ClientProcessIdTest.java @@ -42,6 +42,7 @@ public class ClientProcessIdTest extends AbstractTest { @Test @Tag(Constants.xAzureSQLDW) + @Tag(Constants.xJDBC42) public void testClientProcessId() throws SQLException { SQLServerDataSource ds = new SQLServerDataSource(); ds.setURL(connectionString); @@ -50,7 +51,7 @@ public void testClientProcessId() throws SQLException { try (Connection con = ds.getConnection(); Statement stmt = con.createStatement()) { try (ResultSet rs = stmt.executeQuery(sqlSelect)) { if (rs.next()) { - assertEquals(rs.getInt(1), pid); + assertEquals(pid, rs.getInt(1)); } else { assertTrue(false, "Expected row of data was not found."); } From afa942378ec4bbc93a5d6d7532e90851733fbc19 Mon Sep 17 00:00:00 2001 From: David Engel Date: Fri, 30 Jul 2021 13:00:44 -0700 Subject: [PATCH 4/4] Adding excludes to address PR comments --- pom.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index cf2341605c..11bbd127e1 100644 --- a/pom.xml +++ b/pom.xml @@ -251,11 +251,13 @@ 3.8.0 - **/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java + **/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java **/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java + **/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java **/com/microsoft/sqlserver/jdbc/connection/ConnectionWrapper43Test.java + **/com/microsoft/sqlserver/jdbc/connection/RequestBoundaryMethodsTest.java **/com/microsoft/sqlserver/jdbc/JDBC43Test.java 1.8