Level2 Caching In Hibernate

Hibernate Caching

Hibernate uses two different caches for objects: first-level cache and second-level cache.
First-level cache is associated with the Session object, while second-level cache is associated with the Session Factory 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. To reduce database traffic, second-level cache keeps loaded objects at the Session Factory level between transactions. These objects are available to the whole application, not just to the user running the query. This way, each time a query returns an object that is already loaded in the cache, one or more database transactions potentially are avoided.

In addition, you can use a query-level cache if you need to cache actual query results, rather than just persistent objects.

Cache Implementations
You have the option to tell Hibernate which caching implementation to use by specifying the name of a class that implements org.hibernate.cache.CacheProvider using the property hibernate.cache.provider_class. Hibernate comes bundled with a number of built-in integrations with open-source cache providers (listed below);. Hibernate supports the following open-source cache implementations out-of-the-box:
  • EHCache (org.hibernate.cache.EhCacheProvider)
  • OSCache (org.hibernate.cache.OSCacheProvider)
  • SwarmCache (org.hibernate.cache.SwarmCacheProvider)
  • JBoss TreeCache ( org.hibernate.cache.TreeCacheProvider)

Each cache provides different capacities in terms of performance, memory use, and configuration possibilities:

  • EHCache is a fast, lightweight, and easy-to-use in-process cache. It supports read-only and read/write caching, and memory- and disk-based caching. However, it does not support clustering.
  • OSCache is another open-source caching solution. It is part of a larger package, which also provides caching functionalities for JSP pages or arbitrary objects. It is a powerful and flexible package, which, like EHCache, supports read-only and read/write caching, and memory- and disk-based caching. It also provides basic support for clustering via either JavaGroups or JMS.
  • SwarmCache is a simple cluster-based caching solution based on JavaGroups. It supports read-only or nonstrict read/write caching (the next section explains this term). This type of cache is appropriate for applications that typically have many more read operations than write operations.
  • JBoss TreeCache is a powerful replicated (synchronous or asynchronous) and transactional cache. Use this solution if you really need a true transaction-capable caching architecture.

The <cache> element of a class or collection mapping has the following form:

<cache
    usage="transactional|read-write|nonstrict-read-write|read-only"  
(1)

region="RegionName" (2)
include="all|non-lazy" (3)

/>
  • usage (required) specifies the caching strategy: transactional, read-write, nonstrict-read-write or read-only

  • region (optional, defaults to the class or collection role name) specifies the name of the second level cache region
  • include (optional, defaults to all) non-lazy specifies that properties of the entity mapped with lazy="true" may not be cached when attribute-level lazy fetching is enabled  
Other wise we can specify the cache details to hibernate as <class-cache> and <collection-cache> elements in hibernate.cfg.xml.


The usage attribute specifies a cache concurrency strategy.The following are the summary of the four types of strategies.
1) Read Only: if your application needs to read but never modify instances of a persistent class, a read-only cache may be used. This is the simplest and best performing strategy. It's even perfectly safe for use in a cluster

2) If the application needs to update data, a read-write cache might be appropriate. This cache strategy should never be used if serializable transaction isolation level is required. If the cache is used in a JTA environment, you must specify the property hibernate.transaction.manager_lookup_class, naming a strategy for obtaining the JTA TransactionManager. In other environments, you should ensure that the transaction is completed when Session.close() or Session.disconnect() is called

--
Sidd
http://javaexp.blogspot.com/

Comments

  1. If you copy from Hibernate documentation , at least copy all the information.

    ReplyDelete
  2. Hi, I have created a post [here](http://anirbanchowdhury.wordpress.com/2012/07/23/hibernate-second-level-cache-ehcache/)
    explaining the differences. Please have a look and feel free to comment.

    ReplyDelete

Post a Comment

Popular posts from this blog

Converting Java Map to String

Difference between volatile and synchronized

Invoking EJB deployed on a remote machine