Clearing all statistics for the whole 2nd level cache can be done as follows (if there is any issue with this way to do the clearing, please be so kind to point it out):
Session session = entityManager.unwrap(Session.class);
SessionFactory sessionFactory = session.getSessionFactory();
Statistics statistics = sessionFactory.getStatistics();
But how can I clear the statistics for a specific region only?
Using Ehcache 3 all caches get registered as
CacheStatisticsMXBean. Unfortunately, accessing this beans through the
MBeanServer and clearing them directly messes up statistics data after clearing the region stats for the second time. I can’t tell why, but I can tell that doing so seems to interfere with Hibernate’s statistic computing.
Anyhow, I can also access region statistics through Hibernate:
CacheRegionStatistics regionStatistics = statistics.getCacheRegionStatistics("email-status-cache");
But I haven’t found a way to clear the
CacheRegionStatistics (yet). Has anybody any ideas how to achieve this?
Many thanks for any help
You’ll have to iterate through all entity, natural id and collection caches, accessing the statistics object for every such object through the respective name and check if
CacheableDataStatistics#getRegionName() matches your expected region name.
Take a look into how things are stored in
org.hibernate.stat.internal.StatisticsImpl, then you should be able to figure out how to access every cache to check whether it matches the region name.
thanks for your reply. Maybe this is a dumb question, so please be a little patient with me, but how can I get all entity, natural id and collection caches? I have found
sessionFactory.getStatistics().getSecondLevelCacheRegionNames() to obtain the region names, but this array doesn’t reflect all initilized caches (may due to backwards compatibility as the info says?).
As pointed out in my initial post, I get to the
SessionFactory and from there I somehow have to reach the caches and their statistics.
SessionFactory only delivers
#getCache() which is the complete 2nd level cache but with no chance to reach the unterlying regions.
Also: I’m still a bit confused how to clear the stats even when I manage to obtain the specific
*StatisticsImpl. Do I have to manually clear all
LongAdder? This would be a little weird, but if that’s the way, please let me know.
May I ask you to just post a few code snippets that might help me?
Many thanks and have a nice weekend
Well, through normal means you can’t clear that stuff. You will have to use reflection to access internals and do the clearing if you really want to do this. I can’t share a code snippet as this depends on how you want to do it. Access the fields and reset them.
The other alternative is that you provide a custom
StatistictImplementor implementation and offer a method to do this without using reflection. Set the property
hibernate.stats.factory to a class name that implements the
org.hibernate.stat.spi.StatisticsFactory interface. There you can build a custom
StatisticsImplementor and setup everything the way you want.
Well, many thanks for the insights. I somehow already thought that this would only be possible with reflection. However, since it would only be for developing and observation purpose I’ll spare myself the effort on that. But the other option I will keep in mind for the case I’d need it some day.
Thanks again for clearing this up