Take the 2-minute tour ×

I am using Ehcache in Hibernate. How can i notify the cache that the database is changed. How can i invalidate the data is cached. How can i programatically acheive this?

share|improve this question
add comment

2 Answers

What do exactly mean? There are 2 ways to make a change in the database: inside the app & outside the app

Inside the app you can trigger easily an invalidation. The second one is a harder to solve. Cache elements are stored with a key and based on that key you can delete it.

CacheManager manager = CacheManager.getInstance();
cache = manager.getCache(cacheName);



Depending how you configured ehcache of course. But you need to know the name of the cache that holds the object.

share|improve this answer
add comment

This is how you can invalidate all EhCache items without bothering what the individual cache names are:

CacheManager manager = CacheManager.getInstance();

String[] names = manager.getCacheNames();

for (String name : names)
    Cache cache = manager.getCache(name);

share|improve this answer
While this may have worked well at the time of writing, I had to replace .getCache with .getEhcache, and Cache with Ehcache. Otherwise, .getCache returns null even for caches which exist. –  moshen May 10 '13 at 16:15
Didn't work for me as my configurations being hibernate ehcache timeToLiveSeconds not working –  Prayag Upd Sep 22 '13 at 7:45
add comment

Your Answer


