-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Clear hibernate 2th level cache
PostPosted: Mon Jan 31, 2011 3:54 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 11:11 am
Posts: 22
Hi all
We use Hibernate 2th level cache with JBoss, the web application uses clustering.

Is it possible to programmatically clear the 2th tree cash?
When clearing the cache in one node of the cluster will clear all node cache on the cluster?
Thank you.


Top
 Profile  
 
 Post subject: Re: Clear hibernate 2th level cache
PostPosted: Tue Feb 01, 2011 2:37 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 11:11 am
Posts: 22
I found this post:
http://www.mikedesjardins.net/content/2008/10/clearing-hibernate-second-level-caches
http://community.jboss.org/message/564127

Code:
  @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!


Top
 Profile  
 
 Post subject: Re: Clear hibernate 2th level cache
PostPosted: Thu Feb 10, 2011 6:51 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 11:11 am
Posts: 22
If somebody can enlighten on this clear cache, I will be grateful.
What problem can arise when clearing the Hibernate cache like this?


Top
 Profile  
 
 Post subject: Re: Clear hibernate 2th level cache
PostPosted: Wed Feb 16, 2011 10:54 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 11:11 am
Posts: 22
Anyone?


Top
 Profile  
 
 Post subject: Re: Clear hibernate 2th level cache
PostPosted: Tue Sep 16, 2014 2:37 pm 
Newbie

Joined: Wed Aug 11, 2010 5:27 pm
Posts: 5
ep.getCacheAccessStrategy().evictAll();

however, if you are the new version you get back diff objects from getAllClassMeta...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.