Mike Desjardins is an Enterprise Java developer who lives in Portland, Maine, U.S.A. His primary focus is on ORM technologies, Database Design, and Web Services, as well as occasional dabbling in web design. Mike is also a proud parent and avid gardener. Mike is a DZone MVB and is not an employee of DZone and has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

Clearing Hibernate Second-Level Caches

10.24.2008
| 22529 views |
  • submit to reddit

Recently, I wanted to be able to clear out all of the Hibernate caches via JBoss's JMX console. I could have taken the easy way out; we're using EHCache, so it could have been as simple as calling CacheManager.clearAll(). However, that would have tied me to a specific cache provider. We're still evaluating switching to other cache providers. Ideally, my solution would not be dependent on a specific cache implementation.

Hibernate's API does provide a simple way to clear specific caches, but does not provide any method for clearing out all of them. Writing your own is fairly straightforward. First, you obtain all of the entity and collection metadata from the session factory. Next you iterate over the entities, and if the object is cached, you clear out all of the caches associated with the persisted class or collection. Here's the code:

  @PersistenceContext
private EntityManager em;

public void clearHibernateCache() {
Session s = (Session)em.getDelegate();
SessionFactory sf = s.getSessionFactory();

Map classMetadata = sf.getAllClassMetadata();
for (EntityPersister ep : classMetadata.values()) {
if (ep.hasCache()) {
sf.evictEntity(ep.getCache().getRegionName());
}
}

Map collMetadata = sf.getAllCollectionMetadata();
for (AbstractCollectionPersister acp : collMetadata.values()) {
if (acp.hasCache()) {
sf.evictCollection(acp.getCache().getRegionName());
}
}

return;
}

Now, if we decide to switch to a different cache provider, this code will not need to be re-written. Hopefully we won't ever change to a different JPA implementaion. :)

From http://mikedesjardins.us/blog/

 

5
Your rating: None Average: 5 (1 vote)
Published at DZone with permission of Mike Desjardins, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Shashi Velur replied on Fri, 2008/10/24 - 4:35pm

Yeah, we had a similar situation in our application to programmatically clear the Hibernate Level 2 cache only once during the application startup (after making some huge schema/DDL changes).

We got around that by closing the current Sessionfactory and creating a new one. The lifecycle of Hibernate Level 2 cache is dependent upon that of the Hibernate SessionFactory.

For your situation, I guess your approach works best. :)

Galder Zamarreno replied on Wed, 2012/02/22 - 10:45am

In order to have a cache provider independent solution, Hibernate should provide such API. To go dig the cache provider and clear it directly it's hacky to say the least, and not portable once you switch cache providers.

Steve Ebersole replied on Wed, 2012/02/22 - 11:11am

Mike, why not just use the SessionFactory#getCache() API?  In fact the methods you metioned (SessionFactory#evictEntity, etc)  are all deprecated in favor of going through SessionFactory#getCache()...

sf.getCache().evictEntityRegions()
sf.getCache().evictCollectionRegions()
sf.getCache().evictDefaultQueryRegion()
sf.getCache().evictQueryRegions()  

 There was some question initially about whether calling evictQueryRegions() should imply evictDefaultQueryRegion().  As of today it does not.

 There is currently not a way to manually evict the update-timestamp cache

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.