How second level cache works in Hibernate










            
Caching is facility provided by ORM frameworks which help users to get fast running web application, while help framework itself to reduce number of queries made to database in a single transaction. Hibernate also provide this caching functionality, in two layers.
·       Fist level cache: This is enabled by default and works in session scope. This also  a  session   level cache. It  is valid for session available  .
·       Second level cache: This is apart from first level cache which is available to be used globally in session factory scope.



  
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"/>

 Hibernate  2nd  level   cache  not   able  to   aware   if   database change directly .  Time  to  refresh  cache  will  update cache data.




How second level cache works in Hibernate  How   second  level cache works in  Hibernate Reviewed by Mukesh Jha on 7:09 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.