3.2.2.10. Replace JPA/Hibernate Second Level Cache with Infinispan

Summary

JBoss Cache has been replaced by Infinispan for second-level cache (2LC). This requires a change to the persistence.xml file. The syntax is slightly different, depending on if you are using JPA or Hibernate second level cache. These examples assume you are using Hibernate.

This is an example of how properties for second level cache were specified in the persistence.xml file in JBoss Enterprise Application Platform 5.x.
<property name="hibernate.cache.region.factory_class"
     value="org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory"/>
<property name="hibernate.cache.region.jbc2.cachefactory" value="java:CacheManager"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.region.jbc2.cfg.entity" value="mvcc-entity"/>
<property name="hibernate.cache.region_prefix" value="services"/>
The following steps will use this example to configure Infinispan in JBoss Enterprise Application Platform 6.

Procedure 3.17. Modify the persistence.xml file to use Infinispan

  1. Configure Infinispan for a JPA application in JBoss Enterprise Application Platform 6

    This is how you specify properties to achieve the same configuration for a JPA application using Infinispan in JBoss Enterprise Application Platform 6:
    <property name="hibernate.cache.use_second_level_cache" value="true"/>
    
    In addition, you need to specify a shared-cache-mode with a value of ENABLE_SELECTIVE or ALL as follows:
    • ENABLE_SELECTIVE is the default and recommended value. It means entities are not cached unless you explicitly mark them as cacheable.
      <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
      
    • ALL means entities are always cached even if you mark them as not cacheable.
      <shared-cache-mode>ALL</shared-cache-mode>
      
  2. Configure Infinispan for a native Hibernate application in JBoss Enterprise Application Platform 6

    This is how you specify the same configuration for a native Hibernate application using Infinispan with JBoss Enterprise Application Platform 6:
    <property name="hibernate.cache.region.factory_class"
         value="org.jboss.as.jpa.hibernate4.infinispan.InfinispanRegionFactory"/>
    <property name="hibernate.cache.infinispan.cachemanager"
         value="java:jboss/infinispan/container/hibernate"/>     
    <property name="hibernate.transaction.manager_lookup_class"
         value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
    <property name="hibernate.cache.use_second_level_cache" value="true"/>
    
    
    You must also add the following dependencies to the MANIFEST.MF file:
    Manifest-Version: 1.0
    Dependencies: org.infinispan, org.hibernate
    
For more information about Hibernate cache properties, see: Section 3.2.2.11, “Hibernate Cache Properties”.