While working with Hibernate web applications we will face so many problems in its performance due to database traffic. That to when the database traffic is very heavy . Actually hibernate is well used just because of its high performance only. So some techniques are necessary to maintain its performance. Caching is the best technique to solve this problem. In this article we will discuss about, how we can improve the performance of Hibernate web applications using caching.
Hibernate uses two different caches for objects: first-level cache and second-level cache..
Hibernate uses two different caches for objects: first-level cache and second-level cache..
1.1) First-level cache
First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces then number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.1.2) Second-level cache
Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will available to the entire application, don’t bounds to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cacheworks. Here we can use query level cache also. Later we will discuss about it.2) Cache Implementations
Hibernate supports four open-source cache implementations named EHCache (Easy Hibernate Cache), OSCache (Open Symphony Cache), Swarm Cache, and JBoss Tree Cache. Each cache has differentperformance, memory use, and configuration possibilities.2.1)2.1EHCache(EasyHibernateCache) (org.hibernate.cache.EhCacheProvider)
- It is fast.
- lightweight.
- Easy-to-use.
- Supports read-only and read/write caching.
- Supports memory-based and disk-based caching.
- Does not support clustering.
2.2)OSCache(OpenSymphonyCache) (org.hibernate.cache.OSCacheProvider)
- It is a powerful .
- flexible package
- supports read-only and read/write caching.
- Supports memory- based and disk-based caching.
- Provides basic support for clustering via either JavaGroups or JMS.
2.3)SwarmCache (org.hibernate.cache.SwarmCacheProvider)
- is a cluster-based caching.
- supports read-only or nonstrict read/write caching .
- appropriate for applications those have more read operations than write operations.
2.4)JBoss TreeCache (org.hibernate.cache.TreeCacheProvider)
- is a powerful replicated and transactional cache.
- useful when we need a true transaction-capable caching architecture .
LiveJournal Tags: Hibernate. Cache,ORM
Technorati Tags: Hibernate. Cache,ORM