Add options to configure JPQL comments and slow SQL threshold (#39589)

Closes #39587

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz
2025-06-02 14:55:43 +02:00
committed by GitHub
parent a152fb4a0c
commit ad10cde87e
21 changed files with 126 additions and 14 deletions
@@ -124,17 +124,6 @@
MSSQL returns the whole result at once.
See https://docs.oracle.com/en/database/oracle/oracle-database/21/jjdbc/resultset.html -->
<property name="hibernate.jdbc.fetch_size" value="64" />
<!-- We consider database statements taking longer than 10s as slow
See https://docs.jboss.org/hibernate/orm/6.6/introduction/html_single/Hibernate_Introduction.html#slow-queries
Note, that this requires the logger configuration: org.hibernate.SQL_SLOW:info
-->
<property name="hibernate.log_slow_query" value="10000"/>
<!-- Prepends the name or query text of the executed statement as comment to make it easier to identify slow queries
See https://docs.jboss.org/hibernate/orm/6.6/introduction/html_single/Hibernate_Introduction.html#slow-queries
-->
<property name="hibernate.use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
@@ -88,6 +88,18 @@ public class DatabaseOptions {
.description("The maximum size of the connection pool.")
.build();
public static final Option<Boolean> DB_SQL_JPA_DEBUG = new OptionBuilder<>("db-debug-jpql", Boolean.class)
.category(OptionCategory.DATABASE)
.defaultValue(false)
.description("Add JPQL information as comments to SQL statements to debug JPA SQL statement generation.")
.build();
public static final Option<Integer> DB_SQL_LOG_SLOW_QUERIES = new OptionBuilder<>("db-log-slow-queries-threshold", Integer.class)
.category(OptionCategory.DATABASE)
.description("Log SQL statements slower than the configured threshold with logger org.hibernate.SQL_SLOW and log-level info.")
.defaultValue(10000)
.build();
public static final Option<Boolean> DB_ACTIVE_DATASOURCE = new OptionBuilder<>("db-active-<datasource>", Boolean.class)
.category(OptionCategory.DATABASE_DATASOURCES)
.defaultValue(true)
@@ -159,6 +159,7 @@ import java.util.logging.Handler;
import static org.keycloak.connections.jpa.util.JpaUtils.loadSpecificNamedQueries;
import static org.keycloak.quarkus.runtime.Environment.getCurrentOrCreateFeatureProfile;
import static org.keycloak.quarkus.runtime.Providers.getProviderManager;
import static org.keycloak.quarkus.runtime.configuration.Configuration.getOptionalBooleanKcValue;
import static org.keycloak.quarkus.runtime.configuration.Configuration.getOptionalKcValue;
import static org.keycloak.quarkus.runtime.configuration.Configuration.getOptionalValue;
import static org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX;
@@ -442,6 +443,13 @@ class KeycloakProcessor {
for (Entry<Object, Object> query : loadSpecificNamedQueries(dbKind.toLowerCase()).entrySet()) {
unitProperties.setProperty(QUERY_PROPERTY_PREFIX + query.getKey(), query.getValue().toString());
}
if (getOptionalBooleanKcValue(DatabaseOptions.DB_SQL_JPA_DEBUG.getKey()).orElse(false)) {
unitProperties.put("hibernate.use_sql_comments", "true");
}
getOptionalKcValue(DatabaseOptions.DB_SQL_LOG_SLOW_QUERIES.getKey())
.ifPresent(v -> unitProperties.put("hibernate.log_slow_query", v));
}
private void configureDefaultPersistenceUnitEntities(ParsedPersistenceXmlDescriptor descriptor, CombinedIndexBuildItem indexBuildItem,
@@ -88,6 +88,13 @@ final class DatabasePropertyMappers {
.to("quarkus.datasource.jdbc.max-size")
.paramLabel("size")
.build(),
fromOption(DatabaseOptions.DB_SQL_JPA_DEBUG)
.to("kc." + DatabaseOptions.DB_SQL_JPA_DEBUG.getKey())
.build(),
fromOption(DatabaseOptions.DB_SQL_LOG_SLOW_QUERIES)
.to("kc." + DatabaseOptions.DB_SQL_LOG_SLOW_QUERIES.getKey())
.paramLabel("milliseconds")
.build(),
fromOption(DatabaseOptions.DB_ACTIVE_DATASOURCE)
.to("quarkus.datasource.\"<datasource>\".active")
.build(),
@@ -228,7 +228,7 @@ public class PicocliTest extends AbstractConfigurationTest {
assertEquals(CommandLine.ExitCode.USAGE, nonRunningPicocli.exitCode);
assertThat(nonRunningPicocli.getErrString(), containsString(Help.defaultColorScheme(Help.Ansi.AUTO)
.errorText("Unknown option: '--db-pasword'")
+ "\nPossible solutions: --db-url, --db-url-host, --db-url-database, --db-url-port, --db-url-properties, --db-username, --db-password, --db-schema, --db-pool-initial-size, --db-pool-min-size, --db-pool-max-size, --db-driver, --db"));
+ "\nPossible solutions: --db-url, --db-url-host, --db-url-database, --db-url-port, --db-url-properties, --db-username, --db-password, --db-schema, --db-pool-initial-size, --db-pool-min-size, --db-pool-max-size, --db-debug-jpql, --db-log-slow-queries-threshold, --db-driver, --db"));
}
@Test
@@ -237,7 +237,7 @@ public class PicocliTest extends AbstractConfigurationTest {
assertEquals(CommandLine.ExitCode.USAGE, nonRunningPicocli.exitCode);
assertThat(nonRunningPicocli.getErrString(), containsString(Help.defaultColorScheme(Help.Ansi.AUTO)
.errorText("Unknown option: '--db-pasword'")
+ "\nPossible solutions: --db-url, --db-url-host, --db-url-database, --db-url-port, --db-url-properties, --db-username, --db-password, --db-schema, --db-pool-initial-size, --db-pool-min-size, --db-pool-max-size, --db-driver, --db"));
+ "\nPossible solutions: --db-url, --db-url-host, --db-url-database, --db-url-port, --db-url-properties, --db-username, --db-password, --db-schema, --db-pool-initial-size, --db-pool-min-size, --db-pool-max-size, --db-debug-jpql, --db-log-slow-queries-threshold, --db-driver, --db"));
}
@Test
@@ -247,7 +247,7 @@ public class PicocliTest extends AbstractConfigurationTest {
assertEquals(CommandLine.ExitCode.USAGE, nonRunningPicocli.exitCode);
assertThat(nonRunningPicocli.getErrString(), containsString(Help.defaultColorScheme(Help.Ansi.AUTO)
.errorText("Unknown option: '--db-pasword'")
+ "\nPossible solutions: --db-url, --db-url-host, --db-url-database, --db-url-port, --db-url-properties, --db-username, --db-password, --db-schema, --db-pool-initial-size, --db-pool-min-size, --db-pool-max-size, --db-driver, --db"));
+ "\nPossible solutions: --db-url, --db-url-host, --db-url-database, --db-url-port, --db-url-properties, --db-username, --db-password, --db-schema, --db-pool-initial-size, --db-pool-min-size, --db-pool-max-size, --db-debug-jpql, --db-log-slow-queries-threshold, --db-driver, --db"));
}
@Test
@@ -34,8 +34,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -36,8 +36,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -29,8 +29,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -29,8 +29,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -29,8 +29,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -29,8 +29,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -71,8 +71,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -122,8 +122,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -103,8 +103,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -123,8 +123,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -99,6 +99,12 @@ Config:
Database:
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -119,6 +119,12 @@ Config:
Database:
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -102,8 +102,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -122,8 +122,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -100,8 +100,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>
@@ -120,8 +120,14 @@ Database:
deprecated, you should explicitly specify the db instead. Possible values
are: dev-file, dev-mem, mariadb, mssql, mysql, oracle, postgres. Default:
dev-file.
--db-debug-jpql <true|false>
Add JPQL information as comments to SQL statements to debug JPA SQL statement
generation. Default: false.
--db-driver <driver> The fully qualified class name of the JDBC driver. If not set, a default
driver is set accordingly to the chosen database.
--db-log-slow-queries-threshold <milliseconds>
Log SQL statements slower than the configured threshold with logger org.
hibernate.SQL_SLOW and log-level info. Default: 10000.
--db-password <password>
The password of the database user.
--db-pool-initial-size <size>