How do I enable statistics with Hibernate and JPA?
You can enable Hibernate statistics by setting the property hibernate.generate_statistics in the persistence.xml of your application:
<property name="hibernate.generate_statistics">true</property>
For Hibernate native application, you need to include the above property in hibernate.cfg.xml
After deploying the application, you will see in your server logs the following statistics when the Hibernate session is closed:
18:16:30,502 INFO [org.hibernate.engine.internal.StatisticalLoggingSessionEventListener] (default task-1) Session Metrics {
1238468 nanoseconds spent acquiring 1 JDBC connections;
43165 nanoseconds spent releasing 1 JDBC connections;
1692719 nanoseconds spent preparing 1 JDBC statements;
240264 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
276583 nanoseconds spent executing 1 flushes (flushing a total of 1 entities and 0 collections);
44000 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
It is also possible to gather statistics from WildFly Command Line Interface by digging into the jpa subsystem of your deployment unit. Example:
/deployment=jpa-basic.war/subsystem=jpa/hibernate-persistence-unit=jpa-basic.war#primary:read-resource(include-runtime=true)
{
"outcome" => "success",
"result" => {
"close-statement-count" => 0L,
"collection-fetch-count" => 0L,
"collection-load-count" => 0L,
"collection-recreated-count" => 0L,
"collection-remove-count" => 0L,
"collection-update-count" => 0L,
"completed-transaction-count" => 2L,
"connect-count" => 3L,
"enabled" => true,
"entity-delete-count" => 0L,
"entity-fetch-count" => 0L,
"entity-insert-count" => 1L,
"entity-load-count" => 1L,
"entity-update-count" => 0L,
"flush-count" => 2L,
"hibernate-persistence-unit" => "jpa-basic.war#primary",
"optimistic-failure-count" => 0L,
"prepared-statement-count" => 3L,
"query-cache-hit-count" => 0L,
"query-cache-miss-count" => 0L,
"query-cache-put-count" => 0L,
"query-execution-count" => 1L,
"query-execution-max-time" => 2L,
"query-execution-max-time-query-string" => "FROM SimpleProperty",
"scoped-unit-name" => "jpa-basic.war#primary",
"second-level-cache-hit-count" => 0L,
"second-level-cache-miss-count" => 0L,
"second-level-cache-put-count" => 0L,
"session-close-count" => 3L,
"session-open-count" => 3L,
"statistics-enabled" => true,
"successful-transaction-count" => 3L,
"collection" => undefined,
"entity" => {"com.mastertheboss.model.SimpleProperty" => undefined},
"entity-cache" => {"com.mastertheboss.model.SimpleProperty" => undefined},
"query-cache" => {"FROM_space_SimpleProperty" => undefined}
}
}
The above statistics can also be accessed from JConsole through the jboss.as / <deployment> / jpa / <deployment>#<persistence-unit>. See the following picture:

Enabling Hibernate statistics Programmatically
Finally, it is also possible to enable live statistics programmatically using the Statistics Interface:
Statistics stats = sessionFactory.getStatistics();
stats.setStatisticsEnabled(true);
Transaction tx = session.beginTransaction();
List<Customer> c = session.createQuery("from Customer").list();
for(Customer c : customers){
System.out.println(c);
}
stats.getSessionOpenCount();
stats.logSummary();
session.close();
Recommended Articles
A Simple Guide to Understanding Hibernate vs JPA
Learn the key differences between Hibernate and JPA with this concise guide. #Hibernate #JPA #ORM #Java #CloudNative
Mastering Hibernate ORM and JBoss/WildFly Integration
Learn how to develop Hibernate applications on top of JBoss/WildFly, including standalone Java and Jakarta EE approaches.
Replace Hibernate XML Configuration with JPA Annotations for Enterprise Java Applications
Learn how to use JPA annotations instead of XML files for mapping Entities in your enterprise Java applications. #Hibernate #JPA #Java #CloudNative
Optimizing Database Interactions with Hibernate Annotations: A Comprehensive Guide
Learn about Hibernate annotations for optimizing SQL updates and inserts in Java applications. #Hibernate #JPA #DatabaseOptimization