8000 Require READ_COMMITTED_SNAPSHOT isolation level on MS SQL Server by ahus1 · Pull Request #55789 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions docs/documentation/upgrading/topics/changes/changes-27_0_0.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ Existing client scope assignments are not removed by this change.
Administrators should review dynamically registered clients for unintended scope assignments and remove them through the Admin Console or Admin REST API.
All nodes handling client registration requests must be upgraded for the check to apply consistently.

=== MS SQL Server requires READ_COMMITTED_SNAPSHOT isolation level
// https://github.com/keycloak/keycloak/issues/55788

{project_name} now requires the `READ_COMMITTED_SNAPSHOT` isolation level on MS SQL Server and refuses to start without it.
The previous release logged a warning; this release makes the check mandatory to prevent deadlocks under high load.
Enable it before upgrading by executing:

[source,sql]
----
ALTER DATABASE [your-database-name] SET READ_COMMITTED_SNAPSHOT ON;
----

=== Redirect URI loopback exceptions require literal IP addresses
// https://github.com/keycloak/keycloak/issues/54413

Expand Down
4 changes: 2 additions & 2 deletions docs/guides/server/db.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ This ensures that read operations wait for all pending replication events to be

== Preparing for MS SQL server

On MS SQL Server, the default transaction isolation level is `READ_COMMITTED`, which can lead to deadlocks during high load. Therefore, the recommended isolation level for {project_name} is `READ_COMMITTED_SNAPSHOT`. This isolation level is used by default on Azure SQL.
On MS SQL Server, the default transaction isolation level is `READ_COMMITTED`, which can lead to deadlocks during high load. Therefore, {project_name} requires the `READ_COMMITTED_SNAPSHOT` isolation level and refuses to start without it. This isolation level is used by default on Azure SQL.

However, on MS SQL Server, the database isolation level needs to be modified by executing the following command on your database:
On MS SQL Server, the database isolation level needs to be modified by executing the following command on your database:

[source,sql]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ private void checkMSSQLIsolationLevel() {
String option = rs.getString(1);
String value = rs.getString(2);
if ("isolation level".equalsIgnoreCase(option) && (!"read committed snapshot".equalsIgnoreCase(value))) {
logger.warnf("%s 'isolation level' for database '%s' is set to '%s'. Keycloak recommends 'read committed snapshot' isolation level to avoid deadlocks under high load. Please adjust the isolation level by executing 'ALTER DATABASE %s SET READ_COMMITTED_SNAPSHOT ON'.", vendor, dbName, rs.getString(2), dbName);
throw new ServerStartupError(String.format("%s 'isolation level' for database '%s' is set to '%s'. Keycloak requires 'read committed snapshot' isolation level to avoid deadlocks under high load. Please adjust the isolation level by executing 'ALTER DATABASE %s SET READ_COMMITTED_SNAPSHOT ON'.", vendor, dbName, rs.getString(2), dbName), false);
}
}
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.CLITest;
import org.keycloak.it.junit5.extension.WithDatabase;
import org.keycloak.quarkus.runtime.cli.command.AbstractAutoBuildCommand;

import io.quarkus.test.junit.main.Launch;
import org.junit.jupiter.api.Test;

@CLITest
@WithDatabase(alias = "mssql")
Expand All @@ -38,11 +34,4 @@ protected void assertWrongUsername(CLIResult cliResult) {
protected void assertWrongPassword(CLIResult cliResult) {
cliResult.assertMessage("ErrorCode: 18456");
}

@Test
@Launch({ "start", AbstractAutoBuildCommand.OPTIMIZED_BUILD_OPTION_LONG, "--http-enabled=true", "--hostname-strict=false" })
protected void testWarningIsolationLevel(CLIResult cliResult) {
cliResult.assertMessage("mssql 'isolation level' for database 'master' is set to 'read committed'.");
cliResult.assertStarted();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2026 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.keycloak.it.storage.database.dist;

import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.DistributionTest;
import org.keycloak.it.junit5.extension.WithDatabase;

import io.quarkus.test.junit.main.Launch;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@DistributionTest
@WithDatabase(alias = "mssql-no-rcsi", buildOptions = {"db=mssql"})
@Tag(DistributionTest.STORAGE)
public class MssqlIsolationLevelDistTest {

@Test
@Launch({"start", "--optimized", "--http-enabled=true", "--hostname-strict=false"})
void testFailsWithoutReadCommittedSnapshot(CLIResult cliResult) {
cliResult.assertError("Keycloak requires 'read committed snapshot' isolation level");
cliResult.assertNoMessage("Listening on:");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ private GenericContainer<?> createContainer() {
DockerImageName MYSQL = DockerImageName.parse(MYSQL_IMAGE).asCompatibleSubstituteFor("mysql");
return configureJdbcContainer(new MySQLContainer(MYSQL));
case "mssql":
case "mssql-no-rcsi":
DockerImageName MSSQL = DockerImageName.parse(MSSQL_IMAGE).asCompatibleSubstituteFor("sqlserver");
boolean enableRcsi = "mssql".equals(alias);
return configureJdbcContainer(new MSSQLServerContainer(MSSQL) {
@Override
public void start() {
Expand All @@ -126,6 +128,14 @@ public void start() {
} finally {
mssqlLogger.setLevel(level);
}
if (enableRcsi) {
try {
execInContainer("/opt/mssql-tools18/bin/sqlcmd", "-U", "sa", "-P", getPassword(), "-No", "-Q",
"ALTER DATABASE master SET READ_COMMITTED_SNAPSHOT ON;");
} catch (Exception e) {
throw new RuntimeException("Failed to enable READ_COMMITTED_SNAPSHOT on master", e);
}
}
}
});
case "tidb":
Expand Down
Loading
0