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

Fix | Fixed a warning when retrieving Operating System information from SQL Server Linux #1279

Merged
merged 1 commit into from
Mar 26, 2020
Merged
Changes from all commits
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
57 changes: 24 additions & 33 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public final class SQLServerXAResource implements javax.transaction.xa.XAResourc

private boolean serverInfoRetrieved;
private String version, instanceName;
private int ArchitectureMSSQL, ArchitectureOS;
private int architectureMSSQL, architectureOS;

private static boolean xaInitDone;
private static final Object xaInitLock;
Expand Down Expand Up @@ -201,8 +201,8 @@ public String toString() {
serverInfoRetrieved = false;
version = "0";
instanceName = "";
ArchitectureMSSQL = 0;
ArchitectureOS = 0;
architectureMSSQL = 0;
architectureOS = 0;

}

Expand Down Expand Up @@ -453,22 +453,16 @@ private XAReturnValue DTC_XA_Interface(int nType, Xid xid, int xaFlags) throws X
case XA_START:

if (!serverInfoRetrieved) {
Statement stmt = null;
try {
String query = "select convert(varchar(100), SERVERPROPERTY('Edition'))as edition, "
+ " convert(varchar(100), SERVERPROPERTY('InstanceName'))as instance,"
+ " convert(varchar(100), SERVERPROPERTY('ProductVersion')) as version, @@VERSION;";
try (Statement stmt = controlConnection.createStatement();
ResultSet rs = stmt.executeQuery(query);) {
serverInfoRetrieved = true;
// data are converted to varchar as type variant returned by SERVERPROPERTY is not supported
// by driver
String query = "select convert(varchar(100), SERVERPROPERTY('Edition'))as edition, "
+ " convert(varchar(100), SERVERPROPERTY('InstanceName'))as instance,"
+ " convert(varchar(100), SERVERPROPERTY('ProductVersion')) as version,"
+ " SUBSTRING(@@VERSION, CHARINDEX('<', @@VERSION)+2, 2)";

stmt = controlConnection.createStatement();
ResultSet rs = stmt.executeQuery(query);
rs.next();

String edition = rs.getString(1);
ArchitectureMSSQL = ((null != edition) && (edition.contains("(64-bit)"))) ? 64 : 32;
architectureMSSQL = ((null != edition) && (edition.contains("(64-bit)"))) ? 64 : 32;

// if InstanceName is null use the default instance without name (MSSQLSERVER)
instanceName = (rs.getString(2) == null) ? "MSSQLSERVER" : rs.getString(2);
Expand All @@ -479,27 +473,24 @@ private XAReturnValue DTC_XA_Interface(int nType, Xid xid, int xaFlags) throws X
version = version.substring(0, version.indexOf('.'));
}

// @@VERSION returns single nvarchar string with SQL version, architecture, build date,
// edition and OS version
// Version of the OS running MS SQL is retrieved as substring
ArchitectureOS = Integer.parseInt(rs.getString(4));

rs.close();
/*
* @@VERSION returns single nvarchar string with SQL version, architecture, build date,
* edition and OS version.
*/
String buildInfo = rs.getString(4);
// SQL Server Linux is x64-compatible only.
if (null != buildInfo && buildInfo.contains("Linux")) {
architectureOS = 64;
} else if (null != buildInfo) {
architectureOS = Integer.parseInt(buildInfo.substring(buildInfo.lastIndexOf('<') + 2,
buildInfo.lastIndexOf('>')));
}
}
// Got caught in static analysis. Catch only the thrown exceptions, do not catch
// run time exceptions.
// Catch only the thrown exceptions, do not catch run time exceptions.
catch (Exception e) {
if (xaLogger.isLoggable(Level.WARNING))
xaLogger.warning(
toString() + " Cannot retrieve server information: :" + e.getMessage());
} finally {
if (null != stmt)
try {
stmt.close();
} catch (SQLException e) {
if (xaLogger.isLoggable(Level.FINER))
xaLogger.finer(toString());
}
}
}

Expand All @@ -517,8 +508,8 @@ private XAReturnValue DTC_XA_Interface(int nType, Xid xid, int xaFlags) throws X
cs.setInt(n++, Integer.parseInt(version)); // Version of SQL Server
cs.setInt(n++, instanceName.length()); // Length of SQL Server instance name
cs.setBytes(n++, instanceName.getBytes()); // SQL Server instance name
cs.setInt(n++, ArchitectureMSSQL); // Architecture of SQL Server
cs.setInt(n++, ArchitectureOS); // Architecture of OS running SQL Server
cs.setInt(n++, architectureMSSQL); // Architecture of SQL Server
cs.setInt(n++, architectureOS); // Architecture of OS running SQL Server
cs.setInt(n++, isTransacrionTimeoutSet); // pass 1 if setTransactionTimeout() is called
cs.registerOutParameter(n++, Types.BINARY); // Return UoW

Expand Down