我使用二级缓存和查询缓存。我可以知道如何以编程方式清除所有缓存吗?
发布于 2010-03-17 17:33:53
要清除会话缓存,请使用session.clear()
要清除二级缓存,请使用this code snippet
发布于 2010-03-18 04:10:16
如果您插入Terracotta,您还可以运行Terracotta Dev控制台,该控制台可以检查有关缓存的统计信息,打开和关闭缓存,并从用户界面中清除缓存内容。
此功能也可从JMX bean中获得。
发布于 2019-08-31 04:04:29
@Dino的答案对我来说几乎是有效的,但我从sessionFactory.getCurrentSession()
得到了一个错误(没有配置currentSessionContext!)。我发现这对我有效:
// 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.
}
https://stackoverflow.com/questions/2461063
复制相似问题