简体   繁体   中英

Hibernate : Invalidating Second Level cache

I understood that L2 cache can't update itself if any update happens in the persistent store. So, Should I call evict2ndLevelCache() method after every write or update methods at DAO layer? Or there is a better strategy?

http://howtodoinjava.com/2013/07/02/how-hibernate-second-level-cache-works/

If some user or process make changes directly in database, the there is no way that second level cache update itself until “timeToLiveSeconds” duration has passed for that cache region. In this case, it is good idea to invalidate whole cache and let hibernate build its cache once again. You can use below code snippet to invalidate whole hibernate second level cache.

If you are doing changes in Hibernate entities itself, you don't have to do anything else to ensure the consistency of L2 cache, Hibernate will take care of it.

If you are doing changes via native queries, then explicitly mention which entities are affected, otherwise Hibernate will invalidate the entire second-level cache, as explained here .

If you are changing data in the database from another process, then Hibernate is not aware of it, and you will have to define a strategy that best suits your requirements (expiration policies, explicit invalidation called from the outside of the application, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM