Friday, June 1, 2012

Hibernate Caching Statistics

Hibernate supports statistical collection and reporting, which can be logged to profile & optimize the app's caching.

To enable it, it needs to be switched on in the session factory e.g.:
  • sessionFactory.getStatistics().setStatisticsEnabled(true);
Once that is enabled, it is possible to debug out e.g. in a Servlet Filter the statistics by calling:
  • Statistics stats = HibernateUtil.getSessionFactory().getStatistics();
  • stats.getSecondLevelCacheHitCount();
Available statistics include:
  • Second Level Cache hit/miss counts.
  • Entity load/fetch counts.
  • Query Cache hit/miss counts.
  • Query execution counts.

No comments:

Post a Comment