5 Replies Latest reply on Dec 16, 2011 6:03 AM by Galder Zamarreño

    Infinispan with JPA/Hibernate

    Hans Gunnerud Newbie

      Hi !

       

      I am new to Infinispan and am trying to figure out if we can use this technology in our particular use case below. Any hints or help would be very much appreciated.

       

      Our use case is a currrent JBoss/JPA(Hibernate with EHCache for 2nd level caching) based application with an RDBMS as SoR. The 2nd level cache is currently used for read-only configuration data and is local only in a clustered environment. As part of updating to JBoss AS 6 the plan was to switch to Infinispan as 2nd level cache and to use replicated cache with invalidation for some read-mostly data.

       

      For performance issues we also need to handle caching of potentially large data sets that can be frequently updated (mostly trade related data). For this last part we could use 2nd level cache and query caching, but we are considering moving caching of these outside JPA 2nd level caching. These caches could be loaded with current active data (subset) from the DB at a session start and would either be the SoR for these data intra-session or need to be synchronized with updates via JPA during the session. We don't need to worry about updates to the data outside (directly in DB or other applications).

       

      The thought is to have a separate replicated cache with possibly different keys/grouping than the entities and that can be queried intra-session without hitting the DB. I guess it could either be in the same VM as JBoss or dedicated VM outside. I have been trying to read a bit in the Infinispan documentation but I can't figure out the best way to solve this in a reliable and efficient way without affecting too much of the existing code and DB structure. I will be playing a little bit with Infinispan, but would appreciate if anyone could give me some hints if you have been thinking about the same use case. Things that I am a bit confused about is the usage of the "remote second level cache" in this case, and also about how to handle synchronization with the DB (through JPA) in case the cache is the SoR intra-session (will I for instance have to write a new Cache Loader/Store?).

       

      Thank you

        • 1. Re: Infinispan with JPA/Hibernate
          Galder Zamarreño Master

          First of all, if possible I'd suggest updating to JBoss AS7. It's much leaner, easier to test...etc and has Infinispan as 2LC provider too (same as AS6)

           

          I dunno what SoR means for you but if you're planning to update this separate cache within a transaction, embedded option would be better than remote, cos txs are not yet supported in remote.

           

          In embedded mode, replication would only work if the cluster is small and the data is relatively small, otherwise you'll get issues cos its basically total replication. You should check distribution and see if that fits your needs better.

           

          Do you need this trade data to be persisted as well? If it's relatively short lived, you could have other cluster nodes providing backup till data is used and avoid the need to persist it.

          • 2. Re: Infinispan with JPA/Hibernate
            Nicky A Newbie

            Hello,

             

            I need to migrate second level cache from JbossCache(3.2.6.GA) to infinispan.

            I have the following configuration.

            hibernate-core 3.3.1.GA

            jgroups 2.6.20.final

            glassfish server 3

             

             

            I followed all the steps as per https://docs.jboss.org/author/display/ISPN/Using+Infinispan+as+JPA-Hibernate+Second+Level+Cache+Provider

            I first tried to use hibernate-infinispan(4.0.0), but got compilation issues with RegionFactory hence tried with lower version of

            infinispan. I am now using hibernate-infinispan(3.6.8.Final) and infinispan-core(4.2.1.CR1). Following are my configuration files.

             

             

            hibernate.cfg.xml

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

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

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

             

             

            infinispan-configs.xml

            <?xml version="1.0" encoding="UTF-8"?>

            <infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:infinispan:config:4.0">

               <global>

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

                        clusterName="infinispan-hibernate-cluster" distributedSyncTimeout="50000">

                     <!-- Note that the JGroups transport uses sensible defaults if no configuration property is defined. -->

                     <properties>

                        <!-- TODO: Change to udp.xml once streaming transfer requirement has been removed.  -->

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

                     </properties>

                     <!-- See the JGroupsTransport javadocs for more flags -->

                  </transport>

               </global>

             

             

               <default>

                  <!-- Used to register JMX statistics in any available MBean server -->

                  <jmxStatistics enabled="false"/>

             

             

                  <transaction

                        transactionManagerLookupClass="org.infinispan.transaction.lookup.GenericTransactionManagerLookup"

                        syncRollbackPhase="false"

                        syncCommitPhase="false"

                        useEagerLocking="false" eagerLockSingleNode="false"/>

             

             

                   <deadlockDetection enabled="true" spinDuration="1000"/>

             

             

               </default>

               

               <namedCache name="hibernate-cache">

                  <clustering mode="invalidation">

                      <async/>

                  </clustering>

                  <locking isolationLevel="READ_COMMITTED" concurrencyLevel="500000"

                           lockAcquisitionTimeout="15000" useLockStriping="false" />

                  <!-- Eviction configuration.  WakeupInterval defines how often the eviction thread runs, in milliseconds.

                       0 means the eviction thread will never run.  A separate executor is used for eviction in each cache. -->

                  <eviction wakeUpInterval="5000" maxEntries="10000" strategy="LRU"/>

                  <expiration maxIdle="1000000"/>

                  <lazyDeserialization enabled="true"/>

               </namedCache>    

             

             

                <namedCache name="cache2">

                    <clustering mode="replication">

                        <stateRetrieval fetchInMemoryState="true" timeout="20000"/>

                        <sync replTimeout="30000"/>

                    </clustering>

             

             

                    <!-- Eviction configuration.  WakeupInterval defines how often the eviction thread runs, in milliseconds.

                         0 means the eviction thread will never run.  A separate executor is used for eviction in each cache. -->

                    <eviction wakeUpInterval="5000" maxEntries="10000" strategy="LRU"/>

                    <expiration maxIdle="1000000"/>

                    <lazyDeserialization enabled="true"/>

                </namedCache>  

            </infinispan>

             

             

            With this i tried to deploy my application I got jgroups exception, to solve this i changed the jgroups version to

            2.8.0.GA as per http://community.jboss.org/thread/18822. With this jgroups version also i got the following exception

             

             

            Caused by: org.infinispan.CacheException: org.jgroups.ChannelException: unable to setup the protocol stack: org.jgroups.util.Util.createConcurrentMap()Ljava/util/concurrent/ConcurrentMap;

                      at org.infinispan.remoting.transport.jgroups.JGroupsTransport.buildChannel(JGroupsTransport.java:253)

                      at org.infinispan.remoting.transport.jgroups.JGroupsTransport.initChannel(JGroupsTransport.java:203)

                      at org.infinispan.remoting.transport.jgroups.JGroupsTransport.initChannelAndRPCDispatcher(JGroupsTransport.java:219)

                      at org.infinispan.remoting.transport.jgroups.JGroupsTransport.start(JGroupsTransport.java:151)

             

            Hence I changed the jgroups version from 2.8.0.GA to 2.11.0.GA as in the pom.xml file of infinispan-core the jgroups version was 2.11.0.GA

            After all this changes when I try to deploy my application I got the following exception repeatedly

             

             

            [#|2011-12-13T11:06:08.293+0000|WARNING|glassfish3.0.1|org.jgroups.protocols.UDP|_ThreadID=39;_ThreadName=Thread-1;|send buffer of socket java.net.DatagramSocket@1419c51 was set to 640KB, but the OS only allocated 131.07KB. This might lead to performance problems. Please set your max send buffer in the OS correctly (e.g. net.core.wmem_max on Linux)|#]

             

             

            [#|2011-12-13T11:06:08.293+0000|WARNING|glassfish3.0.1|org.jgroups.protocols.UDP|_ThreadID=39;_ThreadName=Thread-1;|receive buffer of socket java.net.DatagramSocket@1419c51 was set to 20MB, but the OS only allocated 131.07KB. This might lead to performance problems. Please set your max receive buffer in the OS correctly (e.g. net.core.rmem_max on Linux)|#]

             

             

            [#|2011-12-13T11:06:08.294+0000|WARNING|glassfish3.0.1|org.jgroups.protocols.UDP|_ThreadID=39;_ThreadName=Thread-1;|send buffer of socket java.net.MulticastSocket@7dc90f was set to 640KB, but the OS only allocated 131.07KB. This might lead to performance problems. Please set your max send buffer in the OS correctly (e.g. net.core.wmem_max on Linux)|#]

             

             

            [#|2011-12-13T11:06:08.294+0000|WARNING|glassfish3.0.1|org.jgroups.protocols.UDP|_ThreadID=39;_ThreadName=Thread-1;|receive buffer of socket java.net.MulticastSocket@7dc90f was set to 25MB, but the OS only allocated 131.07KB. This might lead to performance problems. Please set your max receive buffer in the OS correctly (e.g. net.core.rmem_max on Linux)|#]

             

             

            [#|2011-12-13T11:06:39.232+0000|WARNING|glassfish3.0.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=39;_ThreadName=http-thread-pool-8080-(1);|ApplicationDispatcher[] PWC1231: Servlet.service() for servlet jsp threw exception

            org.infinispan.util.concurrent.TimeoutException: Replication timeout for myComp_235-42673

                      at org.infinispan.remoting.transport.AbstractTransport.parseResponseAndAddToResponseList(AbstractTransport.java:49)

                      at org.infinispan.remoting.transport.jgroups.JGroupsTransport.invokeRemotely(JGroupsTransport.java:414)

                      at org.infinispan.remoting.rpc.RpcManagerImpl.invokeRemotely(RpcManagerImpl.java:101)

                      at org.infinispan.remoting.rpc.RpcManagerImpl.invokeRemotely(RpcManagerImpl.java:125)

                      at org.infinispan.remoting.rpc.RpcManagerImpl.invokeRemotely(RpcManagerImpl.java:230)

                      at org.infinispan.remoting.rpc.RpcManagerImpl.invokeRemotely(RpcManagerImpl.java:217

             

            To access the named cache (cache2) defined in infinispan-configs.xml i used the following code.

             

            // class to get CacheManager instance

            public class CacheInstanceManager extends DefaultCacheManager {

                private static CacheInstanceManager cacheManager = null;

                private static final String DEF_INFINISPAN_CONFIG_RESOURCE = "infinispan-configs.xml";

             

             

                private CacheInstanceManager() throws IOException {

                    super(DEF_INFINISPAN_CONFIG_RESOURCE);

                }

             

             

                public static CacheInstanceManager getInstance() {

                    if (cacheManager == null) {

                        try {

                            cacheManager = new CacheInstanceManager();

                        } catch (IOException e) {

                            throw new SecurityException("Failed to initialize Cache manager");

                        }

                    }

                    return cacheManager;

                }

            }

             

            and then cache is obtain as CacheInstanceManager.getInstance().getCache("cache2")

             

             

            Please advise, if am missing something.

            • 3. Re: Infinispan with JPA/Hibernate
              Galder Zamarreño Master

              First of all, I'd suggest removing hibernate.cache.infinispan.cfg and hibernate.cache.infinispan.entity.cfg unless you really know how the internal caching works. Also, you should not use the Infinispan 2LC for your own caching. Let Hibernate 2LC manage it's own caches with its own config. If you need to make any particular modifications, indidate them and we can help you do them without altering the rest of the internal 2LC caching use cases.

               

              You can still use Infinispan for your own caching if you want, but is that your intention really?

               

              Also, I cannot see where that exception is coming from, whether from your own use of caching, or Hibernate 2LC..

              • 4. Re: Infinispan with JPA/Hibernate
                Nicky A Newbie

                Thanks for your reply.

                 

                I got this esception from my own defined caching.

                 

                If i removed hibernate.cache.infinispan.cfg property, then how would i define the jgroups configuration file. coz i guess the default infinispan-configuration file in the infinispan package will be used which has default jgroups configuration.

                 

                However i need to configure custom cache along with hibernate 2LC for the purpose of caching resource bundle or user principles. So I tried to place as a named cache in infinispan-configs.xml. If this is not the right place where would i configure such cache and how?

                 

                Also, would it be a good idea to try hibernate-infinispan with hibernate-core 3.3.1.GA or should i upgrade hibernate-core to 3.5 or higher which has infinispan integrated into it.

                • 5. Re: Infinispan with JPA/Hibernate
                  Galder Zamarreño Master

                  Nicky A wrote:

                   

                  f i removed hibernate.cache.infinispan.cfg property, then how would i define the jgroups configuration file. coz i guess the default infinispan-configuration file in the infinispan package will be used which has default jgroups configuration.

                  Right, if you're gonna use a different jgroups configuration, then you might need that property. But what is exactly different in what you want at the JGroups level? Maybe it can be controlled by system property? http://community.jboss.org/wiki/SystemProps. Also, if you modify the config file to change the JGroups file for 2LC, only change that and leave the rest as is. There are very valid reasons for the rest of configuration to be that way.

                  Nicky A wrote:

                   

                  However i need to configure custom cache along with hibernate 2LC for the purpose of caching resource bundle or user principles. So I tried to place as a named cache in infinispan-configs.xml. If this is not the right place where would i configure such cache and how?

                  Use a different cache manager and a different Infinispan config, and possibly different JGroups multicast ip+port, plus different cluster name.

                   

                  Nicky A wrote:

                   

                  Also, would it be a good idea to try hibernate-infinispan with hibernate-core 3.3.1.GA or should i upgrade hibernate-core to 3.5 or higher which has infinispan integrated into it.

                   

                  I'd suggest 3.6 at least, and preferably Hibernate 4 if possible.