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

前端 未结 7 1402
长情又很酷
长情又很酷 2020-12-14 01:44

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

相关标签:
7条回答
  • 2020-12-14 02:12

    you can go with this also

    request.getSession().invalidate();      
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); 
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
    
    0 讨论(0)
  • 2020-12-14 02:17

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

    0 讨论(0)
  • 2020-12-14 02:20

    To clear the session cache use session.clear()

    To clear the 2nd level cache use this code snippet

    0 讨论(0)
  • 2020-12-14 02:27

    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.

    0 讨论(0)
  • 2020-12-14 02:29

    @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.
        }
    
    0 讨论(0)
  • 2020-12-14 02:29

    Same as @Dino's answer, shortened syntax for JPA 2.0 API:

    @Autowired
    private EntityManagerFactory entityManagerFactory;
    
    public void clearHibernateCaches() {
        entityManagerFactory.getCache().unwrap(org.hibernate.Cache.class).evictAllRegions();
    }
    
    0 讨论(0)
提交回复
热议问题