From a6745be99d0854eb7773479d721d156f7582ff51 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Thu, 3 Dec 2026 15:20:24 +0100 Subject: [PATCH 1/3] Require READ_COMMITTED_SNAPSHOT isolation level on MS SQL Server Closes #55788 Signed-off-by: Alexander Schwartz Co-Authored-By: Claude Opus 4.6 Signed-off-by: Alexander Schwartz --- .../upgrading/topics/changes/changes-27_0_0.adoc | 12 ++++++++++++ docs/guides/server/db.adoc | 4 ++-- .../jpa/QuarkusJpaConnectionProviderFactory.java | 2 +- .../keycloak/it/storage/database/MssqlSQLTest.java | 6 +++--- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/docs/documentation/upgrading/topics/changes/changes-27_0_0.adoc b/docs/documentation/upgrading/topics/changes/changes-27_0_0.adoc index 29eb92f0da9..eb08f635f7a 100644 --- a/docs/documentation/upgrading/topics/changes/changes-27_0_0.adoc +++ b/docs/documentation/upgrading/topics/changes/changes-27_0_0.adoc @@ -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 diff --git a/docs/guides/server/db.adoc b/docs/guides/server/db.adoc index b0e7d047012..fb06f48e193 100644 --- a/docs/guides/server/db.adoc +++ b/docs/guides/server/db.adoc @@ -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] ---- diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/jpa/QuarkusJpaConnectionProviderFactory.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/jpa/QuarkusJpaConnectionProviderFactory.java index 80c38f83489..ebe306d391d 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/jpa/QuarkusJpaConnectionProviderFactory.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/jpa/QuarkusJpaConnectionProviderFactory.java @@ -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) { diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/MssqlSQLTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/MssqlSQLTest.java index 8d2a100ee29..43f80d77b99 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/MssqlSQLTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/MssqlSQLTest.java @@ -41,8 +41,8 @@ protected void assertWrongPassword(CLIResult cliResult) { @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(); + protected void testFailsWithoutReadCommittedSnapshot(CLIResult cliResult) { + cliResult.assertError("mssql 'isolation level' for database 'master' is set to 'read committed'."); + cliResult.assertNoMessage("Listening on:"); } } From c58a504e5d900f28d89184084835bca0a18bec32 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Thu, 3 Dec 2026 17:19:30 +0100 Subject: [PATCH 2/3] Enable RCSI on MSSQL test container so integration tests pass The MSSQL integration test container now enables READ_COMMITTED_SNAPSHOT on the master database after startup, matching the new requirement. The old warning-only test is removed since the container now has RCSI. Signed-off-by: Alexander Schwartz Co-Authored-By: Claude Opus 4.6 Signed-off-by: Alexander Schwartz --- .../keycloak/it/storage/database/MssqlSQLTest.java | 11 ----------- .../it/junit5/extension/DatabaseContainer.java | 6 ++++++ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/MssqlSQLTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/MssqlSQLTest.java index 43f80d77b99..724a876521b 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/MssqlSQLTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/MssqlSQLTest.java @@ -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") @@ -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 testFailsWithoutReadCommittedSnapshot(CLIResult cliResult) { - cliResult.assertError("mssql 'isolation level' for database 'master' is set to 'read committed'."); - cliResult.assertNoMessage("Listening on:"); - } } diff --git a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java index 45a61aa9d39..dc7cbaa1c33 100644 --- a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java +++ b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java @@ -126,6 +126,12 @@ public void start() { } finally { mssqlLogger.setLevel(level); } + 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": From daeba8eb89c84d72b078d7ed876ee397b168ab40 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Thu, 3 Dec 2026 17:21:51 +0100 Subject: [PATCH 3/3] Add test verifying startup fails without READ_COMMITTED_SNAPSHOT Add mssql-no-rcsi database alias for testing without RCSI enabled. MssqlIsolationLevelDistTest verifies the server refuses to start. Signed-off-by: Alexander Schwartz Co-Authored-By: Claude Opus 4.6 Signed-off-by: Alexander Schwartz --- .../dist/MssqlIsolationLevelDistTest.java | 39 +++++++++++++++++++ .../junit5/extension/DatabaseContainer.java | 14 ++++--- 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/dist/MssqlIsolationLevelDistTest.java diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/dist/MssqlIsolationLevelDistTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/dist/MssqlIsolationLevelDistTest.java new file mode 100644 index 00000000000..e4cd1679f31 --- /dev/null +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/storage/database/dist/MssqlIsolationLevelDistTest.java @@ -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:"); + } +} diff --git a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java index dc7cbaa1c33..60334a5c639 100644 --- a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java +++ b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DatabaseContainer.java @@ -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() { @@ -126,11 +128,13 @@ public void start() { } finally { mssqlLogger.setLevel(level); } - 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); + 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); + } } } });