4 Replies Latest reply on Jul 26, 2013 4:00 AM by Tommy T

    Insert Infinispan as Cache Layer above existing Hibernate/MySQL layer

    Tommy T Newbie

      Hi,

       

      I have an application that persists information to a MySQL using Hibernate/JPA, classes annotated with @Entity etc. It works fine for prototype but is obviously slow.

      I woild like to put a cache layer between my app and the DB and write this information to Infinispan first and then  somehow have Infinispan keep in sync with DB and update the DB.

       

      Is there a pattern available to update my existing code to do this i.e. I don;t wish to get rid of my JPA layer or @Entity classes but I do wish to use Infinispan to improve application performance.

       

      Thanks

      T

        • 1. Re: Insert Infinispan as Cache Layer above existing Hibernate/MySQL layer
          Tommy T Newbie

          I think what i need to do is configure Infinispan as a L2 Cache?

          • 2. Re: Insert Infinispan as Cache Layer above existing Hibernate/MySQL layer
            Tommy T Newbie

            OK...

             

            I'm trying to configure JBoss 7.1 to use Infinispan as L2 Cache as per https://docs.jboss.org/author/display/ISPN/Using+Infinispan+as+JPA-Hibernate+Second+Level+Cache+Provider and http://www.mastertheboss.com/hibernate-cache/using-hibernate-second-level-cache-with-jboss-as-5-6-7

             

            I am running my app in domain mode. When I deploy my app the JNDI lookup for cache manager always fails whether I use

            value="java:CacheManager" / or value="java:CacheManager/entity" or  value="java:jboss/infinispan/hibernate"/>.

             

             

             

            {code}

            [org.hibernate.cache.infinispan.JndiInfinispanRegionFactory] (MSC service thread 1-4) Unable to retrieve CacheManager from JNDI [java:CacheManager/entity]: javax.naming.NameNotFoundException: java:CacheManager/entity

            {code}

             

            However, when i start JConsole I can see that jboss.as:subsystem=infinispan,cache-container=hibernate,invalidation-cache=entity does exist but it has no value for jndiName. This is teh same for all objects under hibernate.

             

            Persistence.xml:

             

            {code:xml}

             

              <persistence-unit name="my-pu" transaction-type="JTA">

                                <provider>org.hibernate.ejb.HibernatePersistence</provider>

                                <jta-data-source>java:jboss/datasources/mydatasrc</jta-data-source>

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

                          <properties>

                                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

                                <property name="hibernate.hbm2ddl.auto" value="update"/>

                                <property name="hibernate.cache.use_second_level_cache" value="true" />

                          <property name="hibernate.cache.use_query_cache" value="true" />    

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

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

                                    <property name="hibernate.cache.region.jbc2.query.localonly" value="true" />

                                </properties>

                      </persistence-unit>

             

            {code:xml}

             

             

            Any help much appreciated

             

            T

             

            Message was edited by: Tommy T

            • 3. Re: Insert Infinispan as Cache Layer above existing Hibernate/MySQL layer
              Tommy T Newbie

              Closer but no cigar yet!.. if i give the /entity cach a jndi like this (I was forced to insert jboss into string)

               

               

              {code:xml}

              <cache-container name="hibernate" default-cache="local-query">

                                  <transport lock-timeout="60000"/>

                                  <local-cache name="local-query">

                                      <transaction mode="NONE"/>

                                      <eviction strategy="LRU" max-entries="10000"/>

                                      <expiration max-idle="100000"/>

                                  </local-cache>

                                  <invalidation-cache name="entity" mode="SYNC" start="EAGER" jndi-name="java:jboss/CacheManager/entity">

                                      <transaction mode="NON_XA"/>

                                      <eviction strategy="LRU" max-entries="10000"/>

                                      <expiration max-idle="100000"/>

                                  </invalidation-cache>{code:xml}

               

               

              then i get an error like this

               

               

              Caused by: javax.persistence.PersistenceException: [PersistenceUnit: my-pu] Unable to build EntityManagerFactory

              Caused by: org.hibernate.cache.CacheException: Unable to start region factory

              Caused by: java.lang.ClassCastException: org.jboss.as.clustering.infinispan.DefaultEmbeddedCacheManager$DelegatingCache cannot be cast to org.infinispan.manager.EmbeddedCacheManager

               

               

               

               

              if i move the jndiname to cache container like this...

               

               

              {code:xml}<cache-container name="hibernate" default-cache="local-query" jndi-name="java:jboss/CacheManager/entity">

                                  <transport lock-timeout="60000"/>

                                  <local-cache name="local-query">

                                      <transaction mode="NONE"/>

                                      <eviction strategy="LRU" max-entries="10000"/>

                                      <expiration max-idle="100000"/>

                                  </local-cache>

                                  <invalidation-cache name="entity" mode="SYNC" start="EAGER" >

                                      <transaction mode="NON_XA"/>

                                      <eviction strategy="LRU" max-entries="10000"/>

                                      <expiration max-idle="100000"/>

                                  </invalidation-cache>{code:xml}

               

               

              Then i get a null pointer on getache when the app deploys

               

               

               

               

                              Caused by: java.lang.NullPointerException

              at org.hibernate.cache.infinispan.InfinispanRegionFactory.getCache(InfinispanRegionFactory.java:445)

                        at org.hibernate.cache.infinispan.InfinispanRegionFactory.buildEntityRegion(InfinispanRegionFactory.java:198)

               

               

               

              I'm running out of ideas..! any help out there? I have updated my persistence.xml to include 'jboss' in jndi name as per domain.xml snippets

              • 4. Re: Insert Infinispan as Cache Layer above existing Hibernate/MySQL layer
                Tommy T Newbie

                I'll move second part of my question to a new question with more appropriate title as "use L2 cache" was the answer to my priginal question