Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly.
Hypersistence Optimizer is that tool!
As already explained, Hibernate provides a very flexible statistics mechanism. The Hibernate metrics can be accessed either programmatically via the org.hibernate.stat.Statistics interface or via JMX (Java Management Extensions).
In this article, you are going to see how you can expose the Hibernate statistics metrics using JMX.
Now, Hibernate is going to collect metrics and expose them via JMX.
Not that due to the HHH-6190 issue, the Hibernate Statistics object was not exposed via JMX. This issue has been fixed in Hibernate ORM 5.4.2, so consider upgrading your Hibernate version if you want to benefit from this feature.
To see the Hibernate statistics metrics via JMX, we need to open JConsole and attach a new connection to our Hibernate application. Afterward, you need to go to the MBeans tab and locate the org.hibernate.core package as illustrated by the following screenshot.
Notice the org.hibernate.stat.internal.StatisticsImpl MBean which provides access to all metrics supported by the Hibernate Statistics interface.
If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.
The advantage of exposing the Hibernate statistics via JMX is that you can further export these metrics to an APM (Application Performance Monitoring) tool which aggregates metrics from different sources (e.g. OS, database, cache) and correlate them so that you get a better insight into the inner workings of your system.
Based on my book, High-Performance Java Persistence, this workshop teaches you various data access performance optimizations from JDBC, to JPA, Hibernate and jOOQ for the major rational database systems (e.g. Oracle, SQL Server, MySQL and PostgreSQL).