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

    Trying to Get Second Level Cahce Entities

    hasan serdar Newbie

      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.