# Hibernate 2nd level cache: Not able to evict

**URL:** <https://discourse.hibernate.org/t/hibernate-2nd-level-cache-not-able-to-evict/574>\
**Category:** Hibernate ORM\
**Created:** [April 18, 2018, 10:01pm UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-not-able-to-evict/574 "2018-04-18T22:01:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tharahan\_Muthu](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@Tharahan\_Muthu](https://discourse.hibernate.org/u/Tharahan_Muthu)\
**Post date:** [April 18, 2018, 10:01pm UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-not-able-to-evict/574/1 "2018-04-18T22:01:01Z")

</div>

I am using Hibernate 4.1.7 and EhCache as second level cache. I am implementing a rest service to clear cahce (evict all regions ) as needed.

Below is code snippet

```auto
org.hibernate.stat.Statistics statistics = HibernateUtil.getSessionFactory().getStatistics();
      statistics.setStatisticsEnabled(true);

      long hits = statistics.getSecondLevelCacheHitCount();
      long misses = statistics.getSecondLevelCacheMissCount();
      long puts = statistics.getSecondLevelCachePutCount();

      logger.info("Hits: " + hits + " Misses: " + misses + " Puts:" + puts);

      cache.evictEntityRegions();

      hits = statistics.getSecondLevelCacheHitCount();
      misses = statistics.getSecondLevelCacheMissCount();
      puts = statistics.getSecondLevelCachePutCount();

      logger.info("Hits: " + hits + " Misses: " + misses + " Puts:" + puts);         

      long hit0 = statistics.getQueryCacheHitCount();
      long miss0 = statistics.getSecondLevelCacheMissCount();

```

Unfortunately, I get same values for hits/misses and puts after I evict all regions.

Appreciated if some can shed some lights.

---

<div class="post-metadata">

**Author:** ![p3consulting](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/p3consulting/32/565_2.png) [@p3consulting](https://discourse.hibernate.org/u/p3consulting)\
**Post date:** [April 19, 2018, 5:27am UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-not-able-to-evict/574/2 "2018-04-19T05:27:33Z")

</div>

And Hibernate is the only client of EhCache in your application?  
(no Spring or other framework caching method results in the same cache? … Hibernate will not evict these entries…)

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [April 19, 2018, 5:41am UTC](https://discourse.hibernate.org/t/hibernate-2nd-level-cache-not-able-to-evict/574/3 "2018-04-19T05:41:34Z")

</div>

> Unfortunately, I get same values for hits/misses and puts after I evict all regions.

That’s because you forgot to clear the `Statistics`:

```
cache.evictEntityRegions();
statistics.clear();

```
