Hibernate : Invalidating Second Level cache

一曲冷凌霜 提交于 2020-01-04 05:55:15

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/34411897/hibernate-invalidating-second-level-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!