How hibernate ensures second level cache is updated with latest data in database

爷,独闯天下 提交于 2019-12-05 02:06:54

Hibernate manages the cache himself, so when you update some entity thru hibernate Session it will invalidate cache entry assocciated with this entity - so cache is always fresh.

If another process (or even second JVM running the same hibernate application) updates record in database, Hibernate on first JVM is unaware of this fact and has stale object in his cache.

However you can use any cache implementation (cache provider) you want. There are many production-ready cache providers that allow you to configure how long given entity will be stored in cache. For example you can configure your cache to invalide all entities after 30 seconds and so on.

If you use EhCache cache provider you can provide such configuration:

<cache name="com.my.company.Entity" 
   maxElementsInMemory="1000" 
   eternal="false" 
   timeToIdleSeconds="7200" 
   timeToLiveSeconds="7200" 
   overflowToDisk="false" 
   memoryStoreEvictionPolicy="LRU"/>

You can find more information abount L2 cache here: http://www.tutorialspoint.com/hibernate/hibernate_caching.htm

however there is a lot of useful tutorials about this.

It doesn't.

If you change data in the database without using hibernate it won't know about it and your cache and the database get out of sync.

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