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 for broken mssql-jdbc.properties location logic #2579

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class ConfigurableRetryLogic {
.getLogger("com.microsoft.sqlserver.jdbc.ConfigurableRetryLogic");
private static final String SEMI_COLON = ";";
private static final String COMMA = ",";
private static final String FORWARD_SLASH = "/";
private static final String EQUALS_SIGN = "=";
private static final String RETRY_EXEC = "retryExec";
private static final String RETRY_CONN = "retryConn";
Expand Down Expand Up @@ -283,16 +282,25 @@ private static void createConnectionRules(LinkedList<String> listOfRules) throws
private static String getCurrentClassPath() throws SQLServerException {
String location = "";
String className = "";
String locationSuffix = "target/classes/";
Jeffery-Wasty marked this conversation as resolved.
Show resolved Hide resolved

try {
className = new Object() {}.getClass().getEnclosingClass().getName();
location = Class.forName(className).getProtectionDomain().getCodeSource().getLocation().getPath();
Jeffery-Wasty marked this conversation as resolved.
Show resolved Hide resolved
location = location.substring(0, location.length() - 16);
URI uri = new URI(location + FORWARD_SLASH);
// When the driver is used as a jar / as a dependency, the above will be the correct "main" directory where
// the props file should be placed (as per documentation). When testing, or building the driver manually,
// the above will return with a suffix "target/classes/" which must be removed, as, with the first case, the
// properties file should be in the main directory.

if (location.endsWith(locationSuffix)) {
Jeffery-Wasty marked this conversation as resolved.
Show resolved Hide resolved
location = location.substring(0, location.length() - locationSuffix.length());
}

URI uri = new URI(location);
return uri.getPath() + DEFAULT_PROPS_FILE; // For now, we only allow "mssql-jdbc.properties" as file name.
} catch (URISyntaxException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_URLInvalid"));
Object[] msgArgs = {location + FORWARD_SLASH};
Object[] msgArgs = {location};
throw new SQLServerException(form.format(msgArgs), null, 0, e);
} catch (ClassNotFoundException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_UnableToFindClass"));
Expand Down
Loading