6 Replies Latest reply on Mar 5, 2013 9:03 AM by serdar

    Trying to Get Second Level Cahce Entities

    serdar

      Hello,

       

      I am a newbie in Jboss and Infinispan and this is my first post

       

      I am interested in infinispan cahce and trying to learn its capabilities and use cases. Trying to understand what is going inside second-level-cache for hibernate. I am using JBoss "jboss-as-web-7.0.2.Final" and standalone.bat.

       

      After a few days of searching, finally I could set the required configurations and inject the infinispan cache container to my code as follows:

       

      @ManagedBean
      public class InfinispanManager<K, V> {
        @Resource(lookup="java:jboss/infinispan/container/hibernate")
        private org.infinispan.manager.CacheContainer container;
        private org.infinispan.Cache<K, V> cache;
         
        @PostConstruct
        public void start() {
          this.cache = this.container.getCache("entity");
        }
        
        public org.infinispan.Cache<K, V> getCache()
        {
                  return cache;
        }
      }
      

       

      I persist my @Cachable @Entity KVPair bean as:

       

      kvPair = new KVPair(key, value);
      entityManager.persist(kvPair);
      

       

      I inject the infinispanManager as:

       

      @Inject
      private InfinispanManager<Object,Object> manager;
      


      But entity cache always seems empty (After a
      entityManager.persist(kvPair) ) when I look as below:

       

      manager.getCache().keySet().isEmpty()
      

       

      Is my approach is correct ? Am I missing something ?

       

      Here is my persistence.xml persistence-unit

       

         <persistence-unit name="bmtDatabase">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
             <!-- the datasource JNDI name as it is configured in the Application Server config files -->
            <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
            <properties>
                <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:jboss/infinispan/container/hibernate" />
                <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                <property name="hibernate.show_sql" value="true" />
            </properties>
         </persistence-unit>
      

       

      Here is my standalone.xml hibernate cahce container:

       

              <subsystem xmlns="urn:jboss:domain:infinispan:1.0" default-cache-container="hibernate">
                  <cache-container name="hibernate" default-cache="local-query" jndi-name="java:jboss/infinispan/container/hibernate">
                      <local-cache name="entity">
                          <eviction strategy="LRU" max-entries="10000"/>
                          <expiration max-idle="100000"/>
                      </local-cache>
                      <local-cache name="local-query">
                          <eviction strategy="LRU" max-entries="10000"/>
                          <expiration max-idle="100000"/>
                      </local-cache>
                      <local-cache name="timestamps">
                          <eviction strategy="NONE"/>
                      </local-cache>
                  </cache-container>
              </subsystem>
      

       

      And this is the tricky part. I should include the infinispan dependicy to my deployment by manifest.mf. I do that by maven in my pom.xml as below:

       

               <plugin>
                  <artifactId>maven-war-plugin</artifactId>
                  <version>2.1.1</version>
                  <configuration>
                     <!-- Java EE 6 doesn't require web.xml, Maven needs to catch
                        up! -->
                     <failOnMissingWebXml>false</failOnMissingWebXml>
                                  <archive>
                                              <manifestEntries>
                                                <Dependencies>org.infinispan export</Dependencies>
                                              </manifestEntries>
                                  </archive>               
                  </configuration>
               </plugin>
      

       

      This post is a question for me and I hope the examples above also may be helpful for someone else.

        • 1. Re: Trying to Get Second Level Cahce Entities
          galder.zamarreno

          Rather than inspecting the cache yourself, the easiest thing to do is to check the 2nd level cache stats via SessionFactory.getStatistics().getSecondLevelCacheStatistics(...) and pass the name of the entity. After a persist, the getPutCount should be 1.

           

          I dunno details about JBoss Web, but with AS 7.1, you don't need to specify the region factory class or any of that stuff, see https://docs.jboss.org/author/display/AS71/JPA+Reference+Guide#JPAReferenceGuide-UsingtheInfinispansecondlevelcache - just set hibernate.cache.use_second_level_cache to true and that's all you need to do. AS7 takes care of the rest. This avoids misconfiguration.

           

          Also, the standalone.xml in AS 7.1 already has a cache container for Hibernate 2LC, so nothing you need to enable there.

          • 2. Re: Trying to Get Second Level Cahce Entities
            serdar

            Thanks for the reply. I can already see the statistics of the 2LC from JBoss Admin console, Put Count increases. What I am corious about is to touch the entities by using the cache not using the EntityManager. Seocondly, I was not sure in default configuration hibernate cache container has a JNDI name. I have used the extra configuration in order to give it a JNDI name but maybe the container which I gave the jndi-name is not the one hibernate uses.

             

            Now I removed the extra configuration in persistence.xml and using default standalone-ha.xml JBoss 7.1.x in a cluster and @Resource(lookup="java:jboss/infinispan/container/hibernate") still works, it is the default name I think, but still manager.getCache().keySet().isEmpty() = true always

            • 3. Re: Trying to Get Second Level Cahce Entities
              galder.zamarreno

              Accessing the cache in 2LC use case directly is something we do not recommend. The keySet is probably empty because the cache your accessing is not the 2LC one.

              • 4. Re: Trying to Get Second Level Cahce Entities
                galder.zamarreno

                If you explain what you're trying to achieve by accessing the cache entries directly, maybe we can explain an alternative way to achieve what you're trying to do.

                • 5. Re: Trying to Get Second Level Cahce Entities
                  serdar

                  Ok let me explain what I want to do:

                   

                  1- I have a big amount of data loaded from database and should be persisted.

                  2- I have to process and modify that data many times in a second.

                  3- Many clients are trying to get the snapshot of the some part of the data in every second.

                  4- I want to distrubute that data so I can scale the process and clients.

                  5- I am planning to make that scalability by distributing my data by infinispan 2LC and parallel process the owned cache data in each node. That is why I want to get the cache in 2LC bypassing the EntityManager.

                   

                  Maybe doing that by 2LC is not a good idea. Maybe I should use infinispan directly and make the database persistence by a infinispan store not by JPA ?

                  • 6. Re: Trying to Get Second Level Cahce Entities
                    serdar

                    Galder, maybe I am trying to re-invent some kind of Distributed Execution Framework or MapReduce model .  I am working on these issues an trying to understand if distributed execution is my solition ? What do you thik ?