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

Remove Java 9-specific class references from the Java 8 jar #1626

Merged
merged 4 commits into from Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -251,8 +251,15 @@
<version>3.8.0</version>
<configuration>
<excludes>
<exclude>**/com/microsoft/sqlserver/jdbc/ISQLServerConnection43.java</exclude>
<exclude>**/com/microsoft/sqlserver/jdbc/SQLServerConnection43.java</exclude>
David-Engel marked this conversation as resolved.
Show resolved Hide resolved
<exclude>**/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java</exclude>
</excludes>
<testExcludes>
David-Engel marked this conversation as resolved.
Show resolved Hide resolved
<exclude>**/com/microsoft/sqlserver/jdbc/connection/ConnectionWrapper43Test.java</exclude>
<exclude>**/com/microsoft/sqlserver/jdbc/connection/RequestBoundaryMethodsTest.java</exclude>
<exclude>**/com/microsoft/sqlserver/jdbc/JDBC43Test.java</exclude>
</testExcludes>
<source>1.8</source>
<target>1.8</target>
</configuration>
Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
Expand Up @@ -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());
Expand Down
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java
Expand Up @@ -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;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java
Expand Up @@ -29,4 +29,26 @@ static final void throwBatchUpdateException(SQLServerException lastError,
throw new BatchUpdateException(lastError.getMessage(), lastError.getSQLState(), lastError.getErrorCode(),
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);
}

/** 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+
}
pid = (pidLong > Integer.MAX_VALUE) ? 0 : (int) pidLong;
}

static int getProcessId() {
return pid;
}
}
Expand Up @@ -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 "
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/com/microsoft/sqlserver/jdbc/Util.java
Expand Up @@ -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> T newInstance(Class<?> returnType, String className, String constructorArg,
Object[] msgArgs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
Expand Down
Expand Up @@ -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);
Expand All @@ -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.");
}
Expand Down