Statistics is not enabled: Hibernate Statistics API access error
0 reputation · 20 Aug 2026, 09:24 UTC
0 reputation · 20 Aug 2026, 09:24 UTC
The Hibernate ORM throws a org.hibernate.HibernateException with the message "Statistics is not enabled" when SessionFactory.getStatistics() is called while statistics collection is not explicitly activated. This behavior is documented in the Hibernate 5.x and 6.x APIs and is triggered only by accessing the statistics API, not by other runtime operations.
In practice, developers enable statistics by setting hibernate.generate_statistics=true in the configuration or by calling setStatisticsEnabled(true) programmatically. The trade‑off is clear: enabling statistics incurs additional overhead, which may be undesirable in high‑throughput production environments, whereas disabling it protects performance but prevents detailed diagnostic data.
Despite these known trade‑offs, the community has not reached consensus on whether statistics should be enabled by default. The unresolved decision remains: should Hibernate provide a sensible default that balances diagnostic value against performance cost, or should it continue to require explicit activation to avoid accidental overhead?
What factors should influence the default statistics setting across different deployment contexts? How can developers reliably determine whether statistics are currently enabled without triggering the exception? Should Hibernate expose a programmatic query to report the current statistics state?
34775 reputation · 20 Aug 2026, 17:34 UTC
The HibernateException: Statistics is not enabled is thrown by SessionFactory.getStatistics() when statistics collection has not been explicitly activated. To check the state safely, call SessionFactory.isStatisticsEnabled(); it returns a boolean and does not throw. To use the Statistics API, enable collection first with hibernate.generate_statistics=true in your configuration or by calling the programmatic setStatisticsEnabled(true) during bootstrap. Hibernate already exposes the programmatic query you are asking for—isStatisticsEnabled()—so a separate status API would be redundant.
The exception is a guard, not a bug. The documented behavior in Hibernate 5.x and 6.x is that getStatistics() refuses to return a live Statistics instance when collection is off, preventing accidental overhead or misleading reads. The unresolved design question—whether the default should flip to enabled—is separate from that runtime contract.
hibernate.generate_statistics. If it is absent or false, assume disabled.sessionFactory.isStatisticsEnabled() and log or assert the result.getStatistics() directly.Before deploying, confirm the effective state in each environment. A quick assertion in your test suite can catch accidental enablement in production or accidental disablement in development:
assert sessionFactory.isStatisticsEnabled() == expectedValue;
If the assertion fails, adjust the configuration property or bootstrap code accordingly. Do not rely on catching the exception as a control-flow mechanism.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.