Available Statistics And Ways To Get Them.
Hibernate up to 4.2 ships with a statistics and metrics API that allows you to figure out a lot about what is happening under the covers. All available counters are described in the Statistics interface API, in three categories:
- Metrics related to the general Session usage, such as number of open sessions, retrieved JDBC connections, etc.
- Metrics related to the entities, collections, queries, and caches as a whole (aka global metrics).
- Detailed metrics related to a particular entity, collection, query or cache region.
Simple getters are used to access the global metrics (i.e. not tied to a particular entity, collection, cache region, etc.). You can access the metrics of a particular entity, collection or cache region through its name, and through its HQL or SQL representation for queries. Please refer to the Statistics, EntityStatistics, CollectionStatistics, SecondLevelCacheStatistics, and QueryStatistics API Javadoc for more information.
All you have to do is enable statistics for the session factory you're interested in and retrieve the statistics to analyze them. There are plenty of examples out there how to use this feature with Spring. The reason is pretty simple: Spring comes with a legendary MBeanExporter which exposes JMX MBeans as Java Objects. And guess what: Hibernate Statistics provides an easy way of exposing them through JMX. But there is no need to use Spring if you just put together some more RedHat magic :)
You basically have two different ways of enabling the statistics in your configured setting. The easiest way is to add a property to your persistence-unit configuration:
<property name="hibernate.generate_statistics" value="true"/>But it is also possible to enable them manually. More details on how to do that can be found on the community wiki and in the performance-monitoring section in the Hibernate documentation.
Enabling and Exposing Statistics By Example
I created a little example standalone Hibernate application with two entities and a main class which is working with hibernate and initializing everything you need to know. Get your hands on it instantly by forking it on GitHub. Here is the little walk-through:
There are the two mandatory entities (Department and Employee) and the META-INF/persistence.xml. This is the basic setting. There is not much magic in here. You can see where to enable the statistics (potentially) in the persistence.xml. The example enables them in the main class JpaTest. But let's start at the beginning. The main method performs the following steps in order:
- Create the EntityManager to use.
- Register the Statistics Mbean we need.
- Initialize the Jolokia Server to expose JMX via JSON for Hawtio
- Does something with the entities.
|Hibernate MBean in JConsole|
JolokiaServerConfig config = new JolokiaServerConfig(new HashMap<String, String>()); JolokiaServer jolokiaServer = new JolokiaServer(config, true); jolokiaServer.start();
|Hawtio Extension in Chrome|
|Browsing JMX MBeans with Hawtio|
That was the short example. Go ahead and look at the GitHub source-code and feel free to look into Hawtio a bit more:
- Read the getting started guide to find out how to download and install Hawtio in your own environment.
- Read up on how to configure Hawtio in various environments, such as configuring security and where Hawtio stores stuff.
- See how to configure Hawtio on WildFly.
- We prefer to use the issue tracker for dealing with ideas and issues, but if you just want to chat about all things Hawtio please join us on the mailing list.
- Find the Hawtio source-code on GitHub.