Skip to content

Commit

Permalink
✨ [source-mssql] skip sql server agent check if EngineEdition == 8 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dwallace0723 authored and xiaohansong committed Feb 27, 2024
1 parent e57cbb8 commit 82e778d
Showing 1 changed file with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,27 @@ protected void assertCdcSchemaQueryable(final JsonNode config, final JdbcDatabas
// todo: ensure this works for Azure managed SQL (since it uses different sql server agent)
protected void assertSqlServerAgentRunning(final JdbcDatabase database) throws SQLException {
try {
final List<JsonNode> queryResponse = database.queryJsons(connection -> {
final String sql =
"SELECT status_desc FROM sys.dm_server_services WHERE [servicename] LIKE 'SQL Server Agent%' OR [servicename] LIKE 'SQL Server 代理%' ";
final PreparedStatement ps = connection.prepareStatement(sql);
LOGGER.info(String.format("Checking that the SQL Server Agent is running using the query: '%s'", sql));
return ps;
}, sourceOperations::rowToJson);

if (!(queryResponse.get(0).get("status_desc").toString().contains("Running"))) {
throw new RuntimeException(String.format(
"The SQL Server Agent is not running. Current state: '%s'. Please check the documentation on ensuring SQL Server Agent is running.",
queryResponse.get(0).get("status_desc").toString()));
// EngineEdition property values can be found at
// https://learn.microsoft.com/en-us/sql/t-sql/functions/serverproperty-transact-sql?view=sql-server-ver16
// SQL Server Agent is always running on SQL Managed Instance:
// https://learn.microsoft.com/en-us/azure/azure-sql/managed-instance/transact-sql-tsql-differences-sql-server?view=azuresql#sql-server-agent
final Integer engineEdition = database.queryInt("SELECT ServerProperty('EngineEdition')");
if (engineEdition == 8) {
LOGGER.info(String.format("SQL Server Agent is assumed to be running when EngineEdition == '%s'", engineEdition));
} else {
final List<JsonNode> queryResponse = database.queryJsons(connection -> {
final String sql =
"SELECT status_desc FROM sys.dm_server_services WHERE [servicename] LIKE 'SQL Server Agent%' OR [servicename] LIKE 'SQL Server 代理%' ";
final PreparedStatement ps = connection.prepareStatement(sql);
LOGGER.info(String.format("Checking that the SQL Server Agent is running using the query: '%s'", sql));
return ps;
}, sourceOperations::rowToJson);

if (!(queryResponse.get(0).get("status_desc").toString().contains("Running"))) {
throw new RuntimeException(String.format(
"The SQL Server Agent is not running. Current state: '%s'. Please check the documentation on ensuring SQL Server Agent is running.",
queryResponse.get(0).get("status_desc").toString()));
}
}
} catch (final Exception e) {
if (e.getCause() != null && e.getCause().getClass().equals(com.microsoft.sqlserver.jdbc.SQLServerException.class)) {
Expand Down

0 comments on commit 82e778d

Please sign in to comment.