What is a second-level cache in Hibernate? ==> Hibernate uses - TopicsExpress



          

What is a second-level cache in Hibernate? ==> Hibernate uses two different caches for objects: first-level cache:First-level cache is associated with the Session object, second-level cache:second-level cache is associated with the SessionFactory object. By default, Hibernate uses first-level cache on a per-transaction basis. Hibernate uses this cache mainly to reduce the number of SQL queries it needs to generate within a given transaction. For example, if an object is modified several times within the same transaction, Hibernate will generate only one SQL UPDATE statement at the end of the transaction, containing all the modifications. The second-level cache needs to be explicitly configured. Hibernate provides a flexible concept to exchange cache providers for the second-level cache. By default Ehcache is used as caching provider. However more sophisticated caching implementation can be used like the distributed JBoss Cache or Oracle Coherence. The Hibernate configuration looks like: true org.hibernate.cache.EhCacheProvider The ehcache.xml can be configured to cache objects of type com.myapp.Order as shown below second-level cache reduces the database traffic by caching loaded objects at the SessionFactory level between transactions. These objects are available to the whole application, not just to the user running the query. The second-level cache exists as long as the session factory is alive. The second-level cache holds on to the data for all properties and associations (and collections if requested) for individual entities that are marked to be cached. It is imperative to implement proper cache expiring strategies as caches are never aware of changes made to the persistent store by another application. he following are the list of possible cache strategies. 1.Read-only: This is useful for data that is read frequently, but never updated. This is the most simplest and best-performing cache strategy. 2.Read/write: Read/write caches may be appropriate if your data needs to be updated. This carry more overhead than read-only caches. In non-JTA environments, each transaction should be completed when session.close() or session.disconnect() is called. 3.Nonstrict read/write: This is most appropriate for data that is read often but only occasionally modified.This strategy does not guarantee that two transactions wont simultaneously modify the same data. 4.Transactional: This is a fully transactional cache that may be used only in a JTA environment. It can be enabled via the Hibernate mapping files as shown below: ....
Posted on: Fri, 29 Aug 2014 05:37:44 +0000

Trending Topics



Recently Viewed Topics




© 2015