I found this post:
http://www.mikedesjardins.net/content/2008/10/clearing-hibernate-second-level-cacheshttp://community.jboss.org/message/564127Code:
@PersistenceContext
private EntityManager em;
public void clearHibernateCache() {
Session s = (Session)em.getDelegate();
SessionFactory sf = s.getSessionFactory();
Map<String,EntityPersister> classMetadata = sf.getAllClassMetadata();
for (EntityPersister ep : classMetadata.values()) {
if (ep.hasCache()) {
sf.evictEntity(ep.getCache().getRegionName());
}
}
Map<String,AbstractCollectionPersister> collMetadata = sf.getAllCollectionMetadata();
for (AbstractCollectionPersister acp : collMetadata.values()) {
if (acp.hasCache()) {
sf.evictCollection(acp.getCache().getRegionName());
}
}
return;
}
The code wont compile, EntityPersister is missing method: getCache()
So i changed it as so:
Code:
public static void clearSecondLevelCache() {
SessionFactoryImplementor factory = getFactory();
// collections
Map<String, CollectionMetadata> roleMap = factory.getAllCollectionMetadata();
for (String roleName : roleMap.keySet()) {
factory.evictCollection(roleName);
}
// entities
Map<String, ClassMetadata> entityMap = factory.getAllClassMetadata();
for (String entityName : entityMap.keySet()) {
factory.evictEntity(entityName);
}
// query
factory.evictQueries();
}
We are facing corrupted cache with JBOSS Tree cache,
This code will run only on manual user request when facing corrupted cache problem (rare)
What will happen to open transactions using the cache?
Thank you!