1 Reply Latest reply on Oct 18, 2012 5:27 AM by suikast42

    Understanding query and entity cahce

    suikast42

      Hi guys,

       

      I acttivate infinispan by adding this entries in my persistence.xml

       

      <!-- Infinispan configurations -->
      <property name="hibernate.cache.use_second_level_cache" value="true" />
      <property name="hibernate.cache.use_query_cache" value="true" />

       

      And I anotate my entity like shown below.

       

       

      @Cacheable
      @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
      public class MyEntity implements Serializable
      {
      ...
      }

       

       

      If I mark a query as cahcebale then I see in the logs file that the cache works. Hibernate goes only one tiime to the DB.

      But if I don't mark the query as cachebale then I though the entity cahce will chache all the results. But here hibernate goes for every query to the DB.

       

      Did I misundertood or miss something ??

       

      I'm Using Jboss 7.1.1.Final

       

      Thanks in advance

        • 1. Re: Understanding query and entity cahce
          suikast42

          Ok I think (or I hope) I'm little bit smarter now.

           

          The query cahce will cahce the results with the same query for example:

           

          Query Cache

           

          ["from Person as p where p.parent.id=? and p.firstName=?", [ 1 , "Joey"] ] -> [  2 ] ]

           

          So any query like above will return the result from the cache. For this I must set query.setCacheable(true); right ??

           

          The entity cache will only cache the Id based access on the entity.

           

          *-----------------------------------------------------*

          |                 Person Data Cache                   |

          |-----------------------------------------------------|

          | 1 -> [ "Giblbert" , "Q" , "Public" , null , [ 2 , 3 ] ] |

          | 2 -> [ "Joey" , "D" , "Public" ,  1   , []        ] |

          | 3 -> [ "Sara" , "N" , "Public" ,  1   , []        ] |

          *-----------------------------------------------------*

           

          personDAO.getById(1) --> For the same session this entity is cahced in L1. If I do a session.clear() or commit the transaction then the result comes from L2 cache.

          personDAO.getByName("Gilbert") --> Excecute every time a SQL query. ( The query is not cached )

           

          Are my assumptions right ?