# How to clear statistics for a specific 2nd level cache region?

**URL:** <https://discourse.hibernate.org/t/how-to-clear-statistics-for-a-specific-2nd-level-cache-region/8554>\
**Category:** Hibernate ORM\
**Created:** [November 15, 2023, 8:18pm UTC](https://discourse.hibernate.org/t/how-to-clear-statistics-for-a-specific-2nd-level-cache-region/8554 "2023-11-15T20:18:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slevin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/slevin/32/2780_2.png) [@Slevin](https://discourse.hibernate.org/u/Slevin)\
**Post date:** [November 15, 2023, 8:18pm UTC](https://discourse.hibernate.org/t/how-to-clear-statistics-for-a-specific-2nd-level-cache-region/8554/1 "2023-11-15T20:18:30Z")

</div>

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):

```auto
Session session = entityManager.unwrap(Session.class);
SessionFactory sessionFactory = session.getSessionFactory();
Statistics statistics = sessionFactory.getStatistics();
statistics.clear();

```

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 16, 2023, 4:41pm UTC](https://discourse.hibernate.org/t/how-to-clear-statistics-for-a-specific-2nd-level-cache-region/8554/2 "2023-11-16T16:41:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Slevin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/slevin/32/2780_2.png) [@Slevin](https://discourse.hibernate.org/u/Slevin)\
**Post date:** [November 17, 2023, 8:26pm UTC](https://discourse.hibernate.org/t/how-to-clear-statistics-for-a-specific-2nd-level-cache-region/8554/3 "2023-11-17T20:26:01Z")

</div>

Hi beikov,

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

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 20, 2023, 4:52pm UTC](https://discourse.hibernate.org/t/how-to-clear-statistics-for-a-specific-2nd-level-cache-region/8554/4 "2023-11-20T16:52:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Slevin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/slevin/32/2780_2.png) [@Slevin](https://discourse.hibernate.org/u/Slevin)\
**Post date:** [November 20, 2023, 5:46pm UTC](https://discourse.hibernate.org/t/how-to-clear-statistics-for-a-specific-2nd-level-cache-region/8554/5 "2023-11-20T17:46:43Z")

</div>

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
