diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java index a8b393a626..a142ab5b7a 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java @@ -60,11 +60,10 @@ import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder; import org.hibernate.service.spi.ServiceRegistryImplementor; +import org.hibernate.stat.Statistics; import org.hibernate.tuple.entity.EntityTuplizer; import org.hibernate.tuple.entity.EntityTuplizerFactory; -import org.jboss.logging.Logger; - import static org.hibernate.cfg.AvailableSettings.ACQUIRE_CONNECTIONS; import static org.hibernate.cfg.AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS; import static org.hibernate.cfg.AvailableSettings.ALLOW_REFRESH_DETACHED_ENTITY; @@ -113,6 +112,7 @@ import static org.hibernate.cfg.AvailableSettings.SESSION_SCOPED_INTERCEPTOR; import static org.hibernate.cfg.AvailableSettings.STATEMENT_BATCH_SIZE; import static org.hibernate.cfg.AvailableSettings.STATEMENT_FETCH_SIZE; import static org.hibernate.cfg.AvailableSettings.STATEMENT_INSPECTOR; +import static org.hibernate.cfg.AvailableSettings.QUERY_STATISTICS_MAX_SIZE; import static org.hibernate.cfg.AvailableSettings.USE_DIRECT_REFERENCE_CACHE_ENTRIES; import static org.hibernate.cfg.AvailableSettings.USE_GET_GENERATED_KEYS; import static org.hibernate.cfg.AvailableSettings.USE_IDENTIFIER_ROLLBACK; @@ -243,6 +243,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { private boolean inClauseParameterPaddingEnabled; private boolean nativeExceptionHandling51Compliance; + private int queryStatisticsMaxSize; @SuppressWarnings({"WeakerAccess", "deprecation"}) public SessionFactoryOptionsBuilder(StandardServiceRegistry serviceRegistry, BootstrapContext context) { @@ -509,6 +510,13 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { configurationSettings, false ); + + this.queryStatisticsMaxSize = ConfigurationHelper.getInt( + QUERY_STATISTICS_MAX_SIZE, + configurationSettings, + Statistics.DEFAULT_QUERY_STATISTICS_MAX_SIZE + ); + if ( context.isJpaBootstrap() && nativeExceptionHandling51Compliance ) { log.nativeExceptionHandling51ComplianceJpaBootstrapping(); this.nativeExceptionHandling51Compliance = false; @@ -1031,6 +1039,11 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { return nativeExceptionHandling51Compliance; } + @Override + public int getQueryStatisticsMaxSize() { + return queryStatisticsMaxSize; + } + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // In-flight mutation access diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryOptions.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryOptions.java index 44b8c5f24c..480d4b1ffc 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryOptions.java @@ -432,4 +432,9 @@ public class AbstractDelegatingSessionFactoryOptions implements SessionFactoryOp public boolean nativeExceptionHandling51Compliance() { return delegate.nativeExceptionHandling51Compliance(); } + + @Override + public int getQueryStatisticsMaxSize() { + return delegate.getQueryStatisticsMaxSize(); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java b/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java index 08c0d66bed..a8e0695f47 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java @@ -35,6 +35,7 @@ import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode; import org.hibernate.query.criteria.LiteralHandlingMode; import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.jdbc.spi.StatementInspector; +import org.hibernate.stat.Statistics; import org.hibernate.tuple.entity.EntityTuplizerFactory; /** @@ -290,4 +291,8 @@ public interface SessionFactoryOptions { default boolean nativeExceptionHandling51Compliance() { return false; } + + default int getQueryStatisticsMaxSize() { + return Statistics.DEFAULT_QUERY_STATISTICS_MAX_SIZE; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index 4d2712bccf..0516ef1832 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -1951,4 +1951,14 @@ public interface AvailableSettings extends org.hibernate.jpa.AvailableSettings { */ String IN_CLAUSE_PARAMETER_PADDING = "hibernate.query.in_clause_parameter_padding"; + /** + * This setting controls the number of {@link org.hibernate.stat.QueryStatistics} entries + * that will be stored by the Hibernate {@link org.hibernate.stat.Statistics} object. + *
+ * The default value is given by the {@link org.hibernate.stat.Statistics#DEFAULT_QUERY_STATISTICS_MAX_SIZE} constant value. + * + * @since 5.4 + */ + String QUERY_STATISTICS_MAX_SIZE = "hibernate.statistics.query_max_size"; + } diff --git a/hibernate-core/src/main/java/org/hibernate/stat/Statistics.java b/hibernate-core/src/main/java/org/hibernate/stat/Statistics.java index 226dd590e8..e20aa3f110 100755 --- a/hibernate-core/src/main/java/org/hibernate/stat/Statistics.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/Statistics.java @@ -15,6 +15,8 @@ package org.hibernate.stat; */ public interface Statistics { + int DEFAULT_QUERY_STATISTICS_MAX_SIZE = 5000; + /** * Are statistics enabled */ diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java index 04043a2d71..6fecd5d15b 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java @@ -17,9 +17,11 @@ import org.hibernate.cache.spi.Region; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.collections.ArrayHelper; +import org.hibernate.internal.util.collections.BoundedConcurrentHashMap; import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.service.Service; +import org.hibernate.stat.Statistics; import org.hibernate.stat.spi.StatisticsImplementor; import static org.hibernate.internal.CoreLogging.messageLogger; @@ -92,7 +94,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { /** * Keyed by query string */ - private final ConcurrentMap