Optimization is not much use without monitoring and access to performance numbers.
Hibernate provides a full range of figures about its internal operations.
Statistics in Hibernate are available per SessionFactory
.
You can access SessionFactory
metrics in two ways.
Your first option is to call sessionFactory.getStatistics()
and
read or display the Statistics
yourself.
Hibernate can also use JMX to publish metrics if you enable the
StatisticsService
MBean. You may enable a single MBean for all your
SessionFactory
or one per factory. See the following code for
minimalistic configuration examples:
// MBean service registration for a specific SessionFactory Hashtable tb = new Hashtable(); tb.put("type", "statistics"); tb.put("sessionFactory", "myFinancialApp"); ObjectName on = new ObjectName("hibernate", tb); // MBean object name StatisticsService stats = new StatisticsService(); // MBean implementation stats.setSessionFactory(sessionFactory); // Bind the stats to a SessionFactory server.registerMBean(stats, on); // Register the Mbean on the server
// MBean service registration for all SessionFactory's Hashtable tb = new Hashtable(); tb.put("type", "statistics"); tb.put("sessionFactory", "all"); ObjectName on = new ObjectName("hibernate", tb); // MBean object name StatisticsService stats = new StatisticsService(); // MBean implementation server.registerMBean(stats, on); // Register the MBean on the server
TODO: This doesn't make sense: In the first case, we retrieve and use the MBean directly. In the second one,
we must give
the JNDI name in which the session factory is held before using it. Use
hibernateStatsBean.setSessionFactoryJNDIName("my/JNDI/Name")
You can (de)activate the monitoring for a SessionFactory
at configuration time, set hibernate.generate_statistics
to false
at runtime: sf.getStatistics().setStatisticsEnabled(true)
or hibernateStatsBean.setStatisticsEnabled(true)
Statistics can be reset programmatically using the clear()
method.
A summary can be sent to a logger (info level) using the logSummary()
method.
Hibernate provides a number of metrics, from very basic to the specialized information
only relevant in certain scenarios. 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 he entities, collections, queries, and caches as a whole (aka global metrics),
Detailed metrics related to a particular entity, collection, query or cache region.
For example, you can check the cache hit, miss, and put ratio of entities, collections and queries, and the average time a query needs. Beware that the number of milliseconds is subject to approximation in Java. Hibernate is tied to the JVM precision, on some platforms this might even only be accurate to 10 seconds.
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. The following
code shows a simple example:
Statistics stats = HibernateUtil.sessionFactory.getStatistics(); double queryCacheHitCount = stats.getQueryCacheHitCount(); double queryCacheMissCount = stats.getQueryCacheMissCount(); double queryCacheHitRatio = queryCacheHitCount / (queryCacheHitCount + queryCacheMissCount); log.info("Query Hit ratio:" + queryCacheHitRatio); EntityStatistics entityStats = stats.getEntityStatistics( Cat.class.getName() ); long changes = entityStats.getInsertCount() + entityStats.getUpdateCount() + entityStats.getDeleteCount(); log.info(Cat.class.getName() + " changed " + changes + "times" );
To work on all entities, collections, queries and region caches, you can retrieve
the list of names of entities, collections, queries and region caches with the
following methods: getQueries()
, getEntityNames()
,
getCollectionRoleNames()
, and
getSecondLevelCacheRegionNames()
.