Hibernate uses two different caches for objects:
first-level cache (at Session Object level as built-in cache in Hibernate)
and
second-level cache.(at SessionFactory Object level as Configurable 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 the 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 cache
works. Here we can use query level cache also. Later we will discuss about it.
* Second level cache is Configurable cache,the third party vendors are supplying various second level caching s/w's for hibernate application.But all these caching s/w related jar files are available in HIBERNATE_HOME/lib.......
Cache Implementations
Hibernate supports four open-source second level cache implementations named
EHCache (Easy Hibernate Cache),
OSCache (Open Symphony Cache),
Swarm Cache, and
JBoss Tree Cache.
Each cache has different performance, memory use, and configuration possibilities.
How To Configure JBoss Tree Cache?
1. Download the latest cache provider jar from JBoss’ Maven2 repository.(jar files are available in your_HIBERNATE_HOME/lib....... )
2.Activate Second Level cache
Modify your hibernate.cfg.xml and enable second level cache:
<property name="hibernate.cache.use_second_level_cache">true</property>
3. Configure the cache provider in hibernate.cfg.xml , using one of the three options below:- If JBoss Cache instance is bound to JMX (i.e. when deploying within JBoss Application Server), select JmxBoundTreeCacheProvider as cache provider and add the cache’s MBean object name:
<property name="hibernate.cache.provider_class">
org.jboss.hibernate.jbc.cacheprovider.JmxBoundTreeCacheProvider
</property>
<property name="hibernate.treecache.mbean.object_name">
portal:service=TreeCache,type=hibernate
</property>
- If JBoss Cache instance is bound to JNDI, select JndiBoundTreeCacheProvider as the cache provider and add the cache’s JNDI name:
<property name="hibernate.cache.provider_class">
org.jboss.hibernate.jbc.cacheprovider.JndiBoundTreeCacheProvider
</property>
<property name="hibernate.cache.jndi">
JndiBoundTreeCacheInstance
</property>
- If running Hibernate and JBoss Cache standalone or within third party Application Server, select TreeCacheProvider as the cache provider and add the path to cache’s configuration file:
<property name="hibernate.cache.provider_class"> org.jboss.hibernate.jbc.cacheprovider.TreeCacheProvider </property> <property name="hibernate.cache.provider_configuration_file_resource_path">
treecache-optimistic.xml or(treecache.xml)</property>
once see treecache.xml file in hibernatehome/project/etc folder and copy this
treecache.xml in our project
Note 1: org.jboss.hibernate.jbc.cacheprovider.TreeCacheProvider is only fully functional since 1.0.1.GA release.
Note 2: In 1.0.0.GA, default value for* hibernate.cache.provider_configuration_file_resource_path was treecache-optimistic.xml but from 1.0.1.GA, this has been changed to treecache.xml to match old standalone standalone JBoss Cache based cache provider. If treecache.xml is not present, old default, treecache-optimistic.xml, is looked up in case it's present. See JBCLUSTER-217 for more information.
Note : once see Hibernate home/etc folder/Hibernate.properties file (search second level cache here)
How to Activate Query Cache in Hibernate:
Query Cache is a sub cache of second level cache,to store Hql Queries related results in the form of hibernate pojo class objectsBy default second level cache does not activate QueryCache .the programmer needs to enable this explicitly by using following property in hibernate.cfg.xml.
<property name="hibernate.cache.use_query_cache"> true </property>
Reference links:
http://community.jboss.org/wiki/NewJBossCache14xBasedHibernate32CacheProvider
http://www.javabeat.net/articles/37-introduction-to-hibernate-caching-1.html
great
ReplyDelete