Wednesday, March 19, 2014

Hibernate second level cache

The second level cache is used across sessions, which also differentiates it from the session cache, which only – as the name says – has session scope. Hibernate provides a flexible concept to exchange cache providers for the second-level cache. By default Hibernate uses Ehcache as caching provider, however JBoss AS 6 and 7 use Infinispan as caching provider.

One difference with the first level cache is that it's not on by default but it needs some configuration in your persistence.xml to tell Hibernate to turn on the cache. The configuration differs depending on the version of JBoss AS used. Let's see each configuration in detail.

Using second level cache with JBoss AS 5


JBoss AS 5 uses JBoss Cache as caching provider. This is the configuration required to turn on the 2nd level cache on JBoss 5:
01.<properties>
02. 
03.<property name="hibernate.cache.use_second_level_cache" value="true"/>
04.<property name="hibernate.cache.use_query_cache" value="true"/>
05. 
06.<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory"/>
07.<property name="hibernate.cache.region.jbc2.cachefactory" value="java:CacheManager"/>
08.<property name="hibernate.cache.region.jbc2.cfg.entity" value="mvcc-entity"/>
09.<property name="hibernate.cache.region.jbc2.cfg.collection" value="mvcc-entity"/>
10.</properties>

By setting the property hibernate.cache.use_second_level_cache to true we are turning on the second-level cache mechanism. The cache, by default, is activated only for entities, so we also need to explicitly set hibernate.cache.use_query_cache to true if we want to cache queries as well.
The second-level cache can be implemented using several different schemas—open source and commercial. In the next property, hibernate.cache.region.factory_class, we are telling Hibernate to use JBoss Cache as the second-level cache implementation.

The next parameter, hibernate.cache.region.jbc2.cachefactory, is specific to the JBoss Cache implementation. It specifies the JNDI name under which the CacheManager to be used is bound. There is no default value, thus the user must specify the property.

The hibernate.cache.region.jbc2.cfg.collection property is also specific to JBoss Cache and details the name of the configuration that should be used for collection caches (in our configuration, mvcc-entity).

Using second level cache with JBoss AS 6



JBoss AS 6 uses Infinispan 4 as cache provider. The required properties for persistence.xml follows here:
1.<properties>
2.<property name="hibernate.cache.use_second_level_cache">true</property>
3.<property name="hibernate.cache.use_query_cache">true</property>
4.<property name="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.JndiInfinispanRegionFactory</property>
5.<property name="hibernate.cache.infinispan.cachemanager">java:CacheManager/entity</property>
6.</properties>
  
The relevant part of this configuration is the cache manager which is bound in the JNDI tree under the name java:CacheManager/entity
The JNDI name to which the hibernate cache container is bound is defined in infinispan-configs.xml:

1.<infinispan-config name="hibernate" jndi-name="java:CacheManager/entity">
2.<infinispan-config xmlns="urn:infinispan:config:4.2">
3....
4.</infinispan-config>
5.</persistence>

Using second level cache with JBoss AS 7


JBoss AS 7 uses Infinispan 5 as caching provider. Here's the suggested configuration:
1.<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
2.<properties>
3.<property name="hibernate.cache.use_second_level_cache" value="true" />
4.<property name="hibernate.cache.use_query_cache" value="true" />    
5.<property name="hibernate.cache.infinispan.cachemanager" value="java:jboss/infinispan/hibernate"/>
6.<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.JndiInfinispanRegionFactory"/>        
7.</properties>

In JBoss 7 the Infinispan cache manager is bound under the java:jboss/infinispan/hibernate name and is configured into the standalone-ha.xml or domain.xml inside the infinispan subsystem:
01.<subsystem xmlns="urn:jboss:domain:infinispan:1.0" default-cache-container="cluster">
02.. . . . . .
03.<cache-container name="hibernate" default-cache="local-query">
04.<invalidation-cache mode="SYNC" name="entity">
05.<locking isolation="REPEATABLE_READ"/>
06.<eviction strategy="LRU" max-entries="10000"/>
07.<expiration max-idle="100000"/>
08.</invalidation-cache>
09.<local-cache name="local-query">
10.<eviction strategy="LRU" max-entries="10000"/>
11.<expiration max-idle="100000"/>
12.</local-cache>
13.<replicated-cache mode="ASYNC" name="timestamps">
14.<eviction strategy="NONE"/>
15.</replicated-cache>
16.</cache-container>
17.</subsystem>

No comments:

Post a Comment