Class HibernateStatisticsConfig
This configuration class activates Hibernate's internal statistics collection, which can be useful for diagnosing performance issues, such as identifying slow queries or excessive database hits. Enabling statistics allows access to detailed metrics about session activity, query execution, cache usage, and more.
While Hibernate statistics are typically disabled in production environments due to their
potential impact on performance, they can be invaluable during development and testing phases. To
enable statistics in a Spring Boot application, set the following property in your
application.properties
or application.yml
:
spring.jpa.properties.hibernate.generate_statistics=true
Additionally, to log detailed statistics, you can adjust the logging level:
logging.level.org.hibernate.stat=DEBUG
For more information on enabling and using Hibernate statistics, refer to the following resources:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final jakarta.persistence.EntityManagerFactory
Interface used to interact with the entity manager factory for the persistence unit. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Enables Hibernate statistics after the bean initialization.
-
Field Details
-
entityManagerFactory
private final jakarta.persistence.EntityManagerFactory entityManagerFactoryInterface used to interact with the entity manager factory for the persistence unit.
-
-
Constructor Details
-
HibernateStatisticsConfig
public HibernateStatisticsConfig()
-
-
Method Details
-
enableStatistics
@PostConstruct public void enableStatistics()Enables Hibernate statistics after the bean initialization.This method unwraps the
EntityManagerFactory
to obtain the underlyingSessionFactory
, then enables its statistics feature. This allows for the collection of various performance metrics, such as query counts, cache hits, and session activity, which can be useful for performance tuning and analysis.Note: Enabling statistics can have a performance overhead. It is recommended to enable them only in development or testing environments.
-