5 Replies Latest reply on Jul 6, 2012 9:59 AM by galder.zamarreno

    Using Infinispan as Hibernate Second Level Cache Provider in Jboss 6

    nedenom

      OK, I've already read this: https://docs.jboss.org/author/display/ISPN/Using+Infinispan+as+JPA-Hibernate+Second+Level+Cache+Provider

       

      Problem is, it doesn't say anything about how to get Infinispan up and running! What config to enable it, what jars to copy?!

       

       

      My module is deployed into a customized server/default profile. Of course, there is nothing about Infinispan here, but I find it in server/all/cluster. OK, but my module should not necessarily be clustered (actually it shall be deployed to both a clustered and non-clustered server), I just want it as a second level cache instead of EHCache.

       

      So I copy the all/cluster (original from download) to my deploy folder + hibernate-infinispan.jar, infinispan-core.jar, jgroups.jar to lib folder.

       

      That doesn't work either:

       

      2012-06-27 13:06:12,086 INFO  [org.hibernate.cache.infinispan.JndiInfinispanRegionFactory] (Thread-2) [] Unable to retrieve CacheManager from JNDI [java:CacheManager/entity]: javax.naming.NameNotFoundException: CacheManager not bound

       

       

      Where to begin???

        • 1. Re: Using Infinispan as Hibernate Second Level Cache Provider in Jboss 6
          amanukyan

          Hi,

           

          For enabling Infinispan as a 2LC for Hibernate, you need to add the following properties to your persistence.xml file (for JPA):

           

          <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory" />

          <property name="hibernate.cache.infinispan.cfg" value="infinispan-config.xml" />

           

          and for native hibernate it would be:

           

          <property name="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.InfinispanRegionFactory</property>

          <property name="hibernate.cache.infinispan.cfg">infinispan-config.xml</property>

           

          The hibernate.cache.infinispan.cfg points you to the infinispan config file which should be located in your classpath (although you may give the full path as well).

           

          Your infinispan config should look something like:

          <infinispan>

              <global></global>

              <namedCache name="myCacheName">

                  <clustering mode="replication">

                      <sync replTimeout="20000"/>

                  </clustering>

          .....

              </namedCache>

          ......

          </infinispan>


          If you would like to have clustered 2LC, then you should also specify transport (JGroups transport) for replicating the second level cache, by adding transport tag to infinispan global tag. It should look like:

           

          <transport

                          transportClass="org.infinispan.remoting.transport.jgroups.JGroupsTransport"

                          clusterName="infinispan-hibernate-cluster"

                          distributedSyncTimeout="50000"

                          strictPeerToPeer="false">

                      <properties>

                          <property name="configurationFile" value="jgroups-tcp.xml"/>

                      </properties>

                  </transport>

           

          In this case you should also add jgroups-tcp.xml file to your classpath or just use the default one comming with jgroups jar file.

           

          For making your entities and collections cachable, you need to add the following tag to your persistence.xml:

          <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

           

          and mark your entities with following annotations:

           

          @Cacheable

          @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)

           

          When you mark the shared cache mode with enable_selective value, this would mean that only that entities would be cached which are marked with @Cacheable annotation (you can read details here - http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html#setup-configuration).

          For caching your entity mappings (e.g. collections) you should also mark them with @Cache annotation.

           

          For caching queries, the property <property name="hibernate.cache.use_query_cache" value="true"/> is not enough. If you use native hibernate, then you need to call setCacheable(true) method during query execution. If you're using JPA and you have named queries, then you need to set hint to cacheable true:

           

          @NamedQueries(

                  {

                          @NamedQuery(

                                  name = "example",

                                  query = "from example as",

                                  hints = { @QueryHint(name = "org.hibernate.cacheable", value = "true")

                                  }

                          )

          }

           

          Finally for making your entities, queries, collections to be cached, you should add the following properties to your persistence.xml:

           

          <property name="hibernate.cache.infinispan.query.cfg" value="myCacheName"/>

          <property name="hibernate.cache.infinispan.entity.cfg" value="otherDefinedCacheName"/>

          <property name="hibernate.cache.infinispan.collection.cfg" value="otherDefinedCacheName"/>

           

          You should use the following jars: hibernate.jar, infinispan-core.jar, jgroups.jar and hibernate-infinispan.jar

           

          You can find the information about all these properties in the same article that you've mentioned in your post.

           

          Hope this would be helpfull.

           

          Best Regards,

          Anna

          • 2. Re: Using Infinispan as Hibernate Second Level Cache Provider in Jboss 6
            nedenom

            Thanks Anna,

             

            Looks like I'm on the right track then...

             

            But why shouldn't I use the JndiInfinispanRegionFactory to get a shared instance, as recommended in the article?

             

             

            JBoss Application Server 6 and 7 deploy a shared Infinispan cache manager that can be used by all services, so when trying to configure applications with Infinispan second level cache, you should use the JNDI name for the cache manager responsible for the second level cache. By default, this is "java:CacheManager/entity"

             

            <!-- If using Hibernate, add to your hibernate.cfg.xml -->

            <property name="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.JndiInfinispanRegionFactory</property>

            <property name="hibernate.cache.infinispan.cachemanager">java:CacheManager/entity</property>

             

            • 3. Re: Using Infinispan as Hibernate Second Level Cache Provider in Jboss 6
              amanukyan

              Hi Andrey,

               

              the solution that I've described allows you to configure Infinispan with AS6 from the scratch.

               

              If you would like to use the AS6 embedded infinispan configuration, there is a good article about Infinispan integration to AS6. Please checkout this:

               

              https://community.jboss.org/wiki/InfinispanIntegrationInAS6

               

              Regards,

              Anna.

              • 4. Re: Using Infinispan as Hibernate Second Level Cache Provider in Jboss 6
                nedenom

                Thanks again Anna,

                 

                Obviously, using the plain InfinispanRegionFactory was not a good idea. Because I have multiple session factories, I end up with 6 cache managers, each believing they are in a cluster of 6, when in reality there is no cluster.

                 

                I've read that article too. It doesn't work. The problem is the jndi name "java:CacheManager/entity" isn't bound even though the  cluster/infinispan-cache-registry.sar is in the deploy folder and the "<infinispan-config name="hibernate" jndi-name="java:CacheManager/entity">" is defined in the infinispan-configs.xml. And I wonder, does it make sense to deploy the cluster folder to a non-clustered server?

                 

                 

                So the problem still persists: how to deploy, bind and use a shared cache manager on a non-clustered server.

                • 5. Re: Using Infinispan as Hibernate Second Level Cache Provider in Jboss 6
                  galder.zamarreno

                  My suggestion: move to AS 7.1, apart from better integration with Infinispan, you get a load of other benefits (speed of startup, lower mem consumption...etc)