Skip to content
Ameen Abozaid
Planned lessonMID-021-L05

Hibernate statistics

  • Lesson 5 of 6
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

measure statements and cache use in tests.

Planned topics

  • hibernate.generate_statistics=true exposes counts of statements, entity loads, flushes, and cache hits.
  • Assert statement counts in tests to catch N+1 regressions.
  • Log SQL with org.hibernate.SQL at debug level rather than show-sql in production.
  • Libraries such as datasource-proxy can also count statements (verify choice before recording).
Draft outline details

Demonstration

java
Statistics stats = sessionFactory.getStatistics();
stats.clear();
service.loadOrders();
assertThat(stats.getPrepareStatementCount()).isLessThanOrEqualTo(2);

Check your understanding

Why assert statement counts in tests?

Show answer

To catch N+1 queries before production.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.