Hibernate 2nd level cache: Not able to evict

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

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.

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

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();