Beruflich Dokumente
Kultur Dokumente
While working with Hibernate web applications we will face so many problems in
its performance due to databasetraffic. 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.
• Introduction.
o First-level cache.
o Second-level cache.
• Cache Implementations.
o EHCache.
o OSCache.
o SwarmCache.
o JBoss TreeCache.
• Caching Stringategies.
o Read-only.
o Read-Write.
o Nonstriict read-write.
o Transactional.
• Configuration.
• <cache> element.
• Caching the queries.
• Custom Cache.
o Configuration.
o Implementation :: ExampleCustomCache.
• Something about Caching.
o Performance.
o About Caching.
• Conclusion.
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 thenumber 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.
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 entireapplication, 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 cache works. Here we can use query level cache also. Later
we will discuss about it.
2) Cache Implementations
• 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.
• 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)
3) Caching Stringategies
Important thing to remembered while studying this one is none of the cache
providers support all of the cache concurrency strategies.
3.1) Read-only
Advantage if this one is, It is safe for using in a cluster. Here is an example for
using the read-only cache strategy.
) Configuration
<hibernate-configuration>
<session-factory>
...
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EHCacheProvider
</property>
...
</session-factory>
</hibernate-configuration>
5) <cache> element
<cache
usage=" caching stringategy"
region="RegionName"
include="all | non-lazy"/>
Until now we saw only caching the transactions. Now we are going to study about
the caching the queries.Suppose some queries are running frequently with same
set of parameters, those queries can be cached. We have to
sethibernate.cache.use_query_cache to true by
calling Query.setCacheable(true) for enabling the query cache. Actually updates in
the queries occur very often. So, for query caching, two cache regions are
necessary.
• For storing the results.( cache identifier values and results of value type only).
• For storing the most recent updates.
Query cache always used second-level cache only. Queries wont cached by
default. Here is an example implementation of query cache.
<property name="hibernate.cache.use_query_cache">true</property>
Then, we can use the setCacheable() method on any query we wish to cache.
7) Custom Cache
To understand the relation between cache and the application the cache
implementation must generate statistics of cache usage.
package examples.ExampleCustomCache;
import net.sf.hibernate.cache;
import java.util;
import org.apache.commons.logging;
return res.toString();
}
return table.get(key);
}
Package examples.ExapleCustomCache;
import java.util;
import net.sf.hibernate.cache;
while (cList.hasMoreElements())
{
Stringing cName = cList.nextElement().toStringing();
aa.append(cName);
ExapleCustomCache myCache =
(ExapleCustomCache)cacheList.get(cName);
aa.append(myCache.lStats());
}
return aa.toStringing();
}
public ExampleCustomCacheProvider()
{
}
}
8) Something about Caching
8.1) Performance
Hibernate provides some metrics for measuring the performance of caching, which
are all described in the Statistics interface API, in three categories:
9) Conclusion
Caching is good one and hibernate found a good way to implement it for improving
its performance in webapplications especially when more database traffic occurs. If
we implement it very correctly, we will get our applications to be running at
their maximum capacities. I will cover more about the caching implementations in
my coming articles. Try to get full coding guidelines before going to implement this.