How to clear all Hibernate cache (ehcache) using Spring?

59,096

Solution 1

To clear the session cache use session.clear()

To clear the 2nd level cache use this code snippet

Solution 2

The code snippet indicated in Bozho answer is deprecated in Hibernate 4.

According to Hibernate JavaDoc, you can use org.hibernate.Cache.evictAllRegions() :

Evict data from all query regions.

Using the API :

Session session = sessionFactory.getCurrentSession();

if (session != null) {
    session.clear(); // internal cache clear
}

Cache cache = sessionFactory.getCache();

if (cache != null) {
    cache.evictAllRegions(); // Evict data from all query regions.
}

Alternatively, you can clear all data from a specific scope :

org.hibernate.Cache.evictCollectionRegions()
org.hibernate.Cache.evictDefaultQueryRegion()
org.hibernate.Cache.evictEntityRegions()
org.hibernate.Cache.evictQueryRegions()
org.hibernate.Cache.evictNaturalIdRegions()

You might want to check the JavaDoc for hibernate Cache interface (Hibernate 4.3).

And also, second-level cache eviction from hibernate dev guide (4.3).

Solution 3

If you plug in Terracotta, you also have the ability to run the Terracotta Dev Console which can inspect statistics about the cache, turn on and turn off the cache, and clear the cache contents from the user interface.

This functionality is also available from JMX beans.

Solution 4

@Dino 's answer almost worked for me but I got an error from sessionFactory.getCurrentSession() (No currentSessionContext configured!). I found this worked for me:

    // Use @Autowired EntityManager em
    em.getEntityManagerFactory().getCache().evictAll();

    // All of the following require org.hibernate imports
    Session session = em.unwrap(Session.class);

    if (session != null) {
        session.clear(); // internal cache clear
    }

    SessionFactory sessionFactory = em.getEntityManagerFactory().unwrap(SessionFactory.class);

    Cache cache = sessionFactory.getCache();

    if (cache != null) {
        cache.evictAllRegions(); // Evict data from all query regions.
    }
Share:
59,096

Related videos on Youtube

cometta
Author by

cometta

Updated on June 03, 2020

Comments

  • cometta
    cometta almost 4 years

    I am using 2nd level cache and query cache. May I know how to programmatically clear all caches ?

  • King Midas
    King Midas over 9 years
    For modern versions of Hibernate, it would be better to follow the @dino's answer.
  • Sachin Verma
    Sachin Verma about 9 years
    HttpSession and hibernate session are different things.
  • Vikas Sharma
    Vikas Sharma about 7 years
    I want to clear cache data from 2nd level cache by calling below method:- sessionFactory.getCache().evictEntityRegions(); I just want to know , is there any harm in doing this? For eg:- What will happen if i try to clear cache in middle of transaction?
  • Dino
    Dino about 7 years
    I guess it depends of your caching strategy and provider. You may need to test it with the chosen one. The reference doc describes the different settings.
  • Vikas Sharma
    Vikas Sharma about 7 years
    I am using @Cache(usage = CacheConcurrencyStrategy.READ_WRITE). I have a case.Suppose if some transaction is running to get Data from and 2nd level cache has data at that time. At the same time, another thread evicts all region caches while the previous transaction is not completed yet. Then what will happen in this case. Can i get null from cache in that transaction and a db hit will occur ? Is there any chance of any issue?
  • Dino
    Dino about 7 years
    You should write a test with your specific context. According to documentation, the read-write seems to match your case but take note that it has requirements, all detailed here (cf. docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html/…)