3 Replies Latest reply on Apr 17, 2012 3:00 AM by galder.zamarreno

    How to check if the query result is cached when query.setHint is set to CacheRetrieveMode "USE"

    usydrajani

      Hi I am trying to cache the entities in second level Cahe.  I have BookEntity and BookSessionBean to add book get the querires done etc.

      I have a listBook() method in BookSssionBean which simply queries the databse in case if the data is not in cache.

       

      public void listbooks() {
                   Cache<Integer, BookEntity> c = cm.getCache("entity");
                   Query q = em.createQuery("SELECT b FROM BookEntity b");
                   
                   q.setHint("javax.persistence.cache.retrieveMode", javax.persistence.CacheRetrieveMode.USE);
                   System.out.println("after q.setHint  " +c.isEmpty());  // the ouput is true, it means cache is empty. here i simply want to see if the data is in cache, even runing the client twice it remains empty.
                   List<BookEntity> li =  (List<BookEntity>) q.getResultList();
                 
                   Integer i = 0;
               /*    for(BookEntity b : li) {
                       c.put(li.get(i).getISBN(), li.get(i));
                      System.out.println("c.get(1)");
                       c.get(1);                System.out.println( c.isEmpty());
                      System.out.println("This is from cache" +c.get(i).authorName);
                       if (i < li.size()) i++ ;
                  } */
                  
                
          }
      

      Please let me know if above   query.setHint is correct there and also how to see the subsequent request for data is actually being answered from the cache.

      When I am putting each data item manually in cache it works but it is still not clear how to do that with query.setHint or em.setproperty....

       

      Thanks

       

      Meena

        • 1. Re: How to check if the query result is cached when query.setHint is set to CacheRetrieveMode "USE"
          galder.zamarreno

          This is not an Infinispan related question, please head to the Hibernate forums to ask about this.

           

          You can see if the cache is in action or not if there're cache hits in the query cache, stats which are accessible via the session factory.

          • 2. Re: How to check if the query result is cached when query.setHint is set to CacheRetrieveMode "USE"
            usydrajani

            Thanks, I just find out that putting this  <property name="hibernate.generate_statistics" value="true" /> in persistence.xml should give the statics.

            My problem is how to use infinispan as a second level cache with EJBs .

            Can you please tell me how the q.setHint in my program will set the queryresult in cache. It seems there should be a way to set entitymanager and cachemanager for the

            Cache<Integer, BookEntity> c

             

            I have got CacheManager cm and Cache<Integer, BookEntity> c  named "entity" and EntityManager em.  These all seems unrelated. In my program I do not see that q.setHint will actually put data in Cache c.

             

            My BookSessionBean.java

             

             

            @Stateless

            @Remote(BookInterface.class)

            public class BookSessionBean implements BookInterface{

            @PersistenceContext (unitName = "InfiniBookSahredCache-ejbPU")

            protected EntityManager em;

            Query q;

             

             

             

            @Resource(name = "CacheManager/entity", mappedName = "java:CacheManager/entity")

            protected    EmbeddedCacheManager cm;

             

              public void listbooks() {

             

                 Cache<Integer, BookEntity> c = cm.getCache("entity");

             

                 q = em.createQuery("SELECT b FROM BookEntity b");

                 q.setHint("javax.persistence.cache.retrieveMode", javax.persistence.CacheRetrieveMode.USE);
                         System.out.println("after q.setHint  " +c.isEmpty());  // the ouput is true, it means cache is empty. here i simply want to see if the data is in cache, even runing the client twice it remains empty.
                         List<BookEntity> li =  (List<BookEntity>) q.getResultList();
                      
                         Integer i = 0;
                     /*    for(BookEntity b : li) {
                             c.put(li.get(i).getISBN(), li.get(i));
                            System.out.println("c.get(1)");
                             c.get(1);                System.out.println( c.isEmpty());
                            System.out.println("This is from cache" +c.get(i).authorName);
                             if (i < li.size()) i++ ;
                        } */

                       
                     
                }

                       

                     

                }

             

             

            My BookEntity.java is

            import java.io.Serializable;

             

            import javax.persistence.Cacheable;

            import javax.persistence.Entity;

            import javax.persistence.GeneratedValue;

            import javax.persistence.GenerationType;

            import javax.persistence.Id;

            import javax.persistence.NamedQueries;

            import javax.persistence.NamedQuery;

            import javax.persistence.Table;

             

            @Entity

            @Cacheable

            @Table(name = "BOOKENTITY")

            //@NamedQueries({@NamedQuery(name="findBookByISBN",query="SELECT b FROM BookEntity b WHERE  b.isbn=:'8')"),

            //@NamedQuery(name="findBookTitle",query="SELECT b FROM BookEntity b WHERE b.title=:bTitle")})

            @NamedQuery(name="findBookTitle",query="SELECT b FROM BookEntity b WHERE b.title=:bTitle")

            public class BookEntity implements Serializable{

               

                String authorName;

                String title;

               

                //@Id @GeneratedValue(strategy=GenerationType.AUTO)

                @Id

                private int ISBN;

               

                public   BookEntity() {

                         }

               

               

                public int getISBN() {

                    return ISBN;

                }

             

                public void setISBN(int ISBN) {

                    this.ISBN = ISBN;

                }

               

                public void setAuthor(String a){

                    this.authorName = a;

                }

               

                public void setTitle(String t){

                    this.title = t;

                }

               

                String getAuthor(String a){

                    return authorName;

                }

               

                String getTitle(String t){

                    return title;

                }

               

             

            }

             

             

             

            My Persistence.xml is

             

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

            <persistence xmlns="http://java.sun.com/xml/ns/persistence"

               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

               version="2.0">

               <persistence-unit name="InfiniBookSahredCache-ejbPU" transaction-type="JTA">

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

                  <jta-data-source>java:/PostgresDS</jta-data-source>

                        <properties>

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

                       <property name="hibernate.session_factory_name" value="SessionFactories/infinispan"/>

                       <property name="javax.persistence.sharedCache.mode" value="ALL"/>

                      

                    

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

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

                <property name="hibernate.generate_statistics" value="true" />

               

               

                <property name="hibernate.cache.region_prefix" value="infinispan" />

               

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

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

                  </properties>

                 

                 

               </persistence-unit>

            </persistence>

             

            Thanks

             

            Meena

            • 3. Re: How to check if the query result is cached when query.setHint is set to CacheRetrieveMode "USE"
              galder.zamarreno

              To cache query results, you need to call query.setCacheable(true);