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 SQLServerDatabaseMetadata.getMaxConnections() API query #1009

Merged
merged 11 commits into from
May 14, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* The API javadoc for JDBC API methods that this class implements are not repeated here. Please see Sun's JDBC API
* interfaces javadoc for those details.
*/
@SuppressWarnings("unused")
public final class SQLServerDatabaseMetaData implements java.sql.DatabaseMetaData, Serializable {
/**
* Always update serialVersionUID when prompted.
Expand Down Expand Up @@ -1014,16 +1015,23 @@ public int getMaxColumnsInTable() throws SQLServerException {
@Override
public int getMaxConnections() throws SQLException, SQLTimeoutException {
checkClosed();
try {
String s = "sp_configure 'user connections'";
SQLServerResultSet rs = getResultSetFromInternalQueries(null, s);
if (!rs.next())
try (SQLServerResultSet rs = getResultSetFromInternalQueries(null,
"select maximum from sys.configurations where name = 'user connections'")) {
if (!rs.next()) {
return 0;
}
return rs.getInt("maximum");
} catch (SQLServerException e) {
return 0;
// Try with sp_configure if users do not have privileges to execute sys.configurations
try (SQLServerResultSet rs1 = getResultSetFromInternalQueries(null, "sp_configure 'user connections'")) {
if (!rs1.next()) {
return 0;
}
return rs1.getInt("maximum");
} catch (SQLServerException e1) {
return 0;
}
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public void testAbortBadParam() throws SQLException {
try {
conn.abort(null);
} catch (SQLException e) {
assertTrue(e.getMessage().contains(TestResource.getResource("R_invalidArgumentExecutor")));
assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_invalidArgument")));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ protected Object[][] getContents() {
{"R_cannotOpenDatabase", "Cannot open database"}, {"R_shouldNotConnect", "Should not have connected"},
{"R_loginFailed", "Login failed"}, {"R_exitedMoreSeconds", "Exited in more than {0} seconds."},
{"R_exitedLessSeconds", "Exited in less than {0} seconds."},
{"R_invalidArgumentExecutor", "The argument executor is not valid"},
{"R_threadInterruptNotSet", "Thread's interrupt status is not set."},
{"R_connectMirrored", "Connecting to a mirrored"},
{"R_trustStorePasswordNotSet", "The DataSource trustStore password needs to be set."},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,10 @@ public void testGetColumnPrivileges() throws SQLException {
public void testGetFunctionsWithWrongParams() throws SQLException {
try (Connection conn = getConnection()) {
conn.getMetaData().getFunctions("", null, "xp_%");
assertTrue(false, TestResource.getResource("R_noSchemaShouldFail"));
} catch (Exception ae) {}
fail(TestResource.getResource("R_noSchemaShouldFail"));
} catch (SQLException e) {
assert (e.getMessage().matches(TestUtils.formatErrorMsg("R_invalidArgument")));
}
}

/**
Expand Down Expand Up @@ -544,4 +546,21 @@ public void testPreparedStatementMetadataCaching() throws SQLException {
}
}
}

@Test
@Tag(Constants.xAzureSQLDW)
public void testGetMaxConnections() throws SQLException {
try (Statement stmt = connection.createStatement(); ResultSet rs = stmt
.executeQuery("select maximum from sys.configurations where name = 'user connections'")) {
assert (null != rs);
rs.next();

DatabaseMetaData databaseMetaData = connection.getMetaData();
int maxConn = databaseMetaData.getMaxConnections();

assertEquals(maxConn, rs.getInt(1));
} catch (SQLException e) {
fail(e.getMessage());
}
}
}