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 @@ -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 @@ -35,6 +35,7 @@
import com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Constants;
Expand Down Expand Up @@ -80,7 +81,8 @@ public void testEncryptedConnection() throws SQLException {
ds.setEncrypt(true);
ds.setTrustServerCertificate(true);
ds.setPacketSize(8192);
try (Connection con = ds.getConnection()) {}
try (Connection con = ds.getConnection()) {
}
}

@Test
Expand All @@ -95,7 +97,8 @@ class MyEventListener implements javax.sql.ConnectionEventListener {
boolean connClosed = false;
boolean errorOccurred = false;

public MyEventListener() {}
public MyEventListener() {
}

public void connectionClosed(ConnectionEvent event) {
connClosed = true;
Expand Down Expand Up @@ -457,7 +460,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 Expand Up @@ -503,7 +506,8 @@ public void run() {
ds.setServerName("invalidServerName" + UUID.randomUUID());
ds.setLoginTimeout(5);

try (Connection con = ds.getConnection()) {} catch (SQLException e) {
try (Connection con = ds.getConnection()) {
} catch (SQLException e) {
isInterrupted = Thread.currentThread().isInterrupted();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,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 @@ -543,4 +545,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());
}
}
}