9 Replies Latest reply on Aug 4, 2008 2:53 PM by pozzo

    Hibernate/Writes but does not loads

    pozzo

      Hi,

      We are trying to use Hibernate with JBoss Cache 1.4 (JBoss 4.2.2).

      We annotated our beans as follow:

      @Entity
      @Table(name="TSC_ATIVO", schema=SchemaNames.ATIVOS)
      @org.hibernate.annotations.Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      public class BaseSecurity implements Serializable


      With configured ThreeCache in the MBean and in the persistence.xml file.

      After making a test that reads a entity, we could check on the JMX Console that the entity was saved on the cache as expected.

      BUT when we try to load the entity, we always see the SQL code going on on the console and it does not read from the cache.

      I already set the cache log level to trace, but could not figure out why it is not reading from the cache.

      Somebody have some idea on how to debug this problem? We thought that it would be very automatic.

      Thanks

      Luciano

        • 1. Re: Hibernate/Writes but does not loads
          skajotde

          you must enable second level cache:


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


          And cache is used in that situations:

          Whenever you pass an object to save(), update() or saveOrUpdate() and whenever you retrieve an object using load(), get(), list(), iterate() or scroll(), that object is added to the internal cache of the Session.
          


          http://www.hibernate.org/hib_docs/reference/en/html/performance.html

          Maybe you are using session.createQuery which uses cache in different ways.

          • 2. Re: Hibernate/Writes but does not loads
            pozzo

            Hi Kamil,

            I add the hibernate.cache.use_second_level_cache property as you said, but without success (I believe that is true by default)

            We are using Hibernate through JPA, so I tried with the find method and createQuery of the EntityManager. In the both executions, the SQL code appears in the console, so, the conclusion is that cache its not working because is going in the database (and the performace do not change too)


            The code:

            Find by id

             public void findByIdTest() {
             long initTime= System.currentTimeMillis();
             Fund f = em.find(Fund.class, 1718);
             System.out.println(f);
             HomeOnShoreFund h = em.find(HomeOnShoreFund.class, 91);
             System.out.println(h);
             MarketOnShoreFund m = em.find(MarketOnShoreFund.class, "143812");
             System.out.println(m);
             System.out.println("Spent:"+(System.currentTimeMillis()-initTime)+"\n");
             }
            



            Find all

             public void findAllTest() {
             long initTime= System.currentTimeMillis();
             List resultList1 = em.createQuery("from " + Fund.class.getName()).getResultList();
             System.out.println("Funds:"+resultList1.size());
             List resultList2 = em.createQuery("from " + MarketOnShoreFund.class.getName()).getResultList();
             List resultList3 = em.createQuery("from " + HomeOnShoreFund.class.getName()).getResultList();
             System.out.println("Spent:"+(System.currentTimeMillis()-initTime)+"\n");
             }
            



            persistence.xml

            <?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_1_0.xsd" version="1.0">
             <persistence-unit name="FrameworkHG_PU">
             <jta-data-source>java:/jca/FrameworkHG-Oracle-DS</jta-data-source>
             <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
             <property name="hibernate.show_sql" value="true" />
             <property name="hibernate.format_sql" value="true" />
             <property name="hibernate.use_sql_comments" value="false" />
             <property name="jboss.entity.manager.jndi.name" value="java:/FrameworkHGEntityManager" />
             <property name="jboss.entity.manager.factory.jndi.name" value="java:/FrameworkHGEntityManagerFactory"/>
             <property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook"/>
             <property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache"/>
             <property name="hibernate.cache.use_second_level_cache" value="true" />
             </properties>
             </persistence-unit>
            </persistence>
            



            Something is wrong?


            thank-you

            • 3. Re: Hibernate/Writes but does not loads
              skajotde

              Looks ok ;)

              Try enable DEBUG logging on org.hibernate.cache category.

              and cache is used in second and next retrieves from databases ;)

              public void findByIdTest() {
              long initTime= System.currentTimeMillis();
              Fund f = em.find(Fund.class, 1718);
              System.out.println(f);
              // Should cause "Cache hit" in logs
              Fund f2 = em.find(Fund.class, 1718);
              System.out.println(f2);
              System.out.println("Spent:"+(System.currentTimeMillis()-initTime)+"\n");
              }

              • 4. Re: Hibernate/Writes but does not loads
                skajotde

                 

                "pozzo" wrote:




                @Entity
                @Table(name="TSC_ATIVO", schema=SchemaNames.ATIVOS)
                @org.hibernate.annotations.Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
                public class BaseSecurity implements Serializable




                Try

                @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region="_default_")
                


                region or regnioName maybe should be _default_ as configured in treecache or something new what you configure.

                • 5. Re: Hibernate/Writes but does not loads
                  pozzo

                  Kamil,

                  It seems that the cache is working, in the log I see 'cache hit'. But I am very disappointed because the performance is equal and in some cases worse than without cache. And the sql queries continue to appear in the console, which seems strange.

                  I know that the cache is not a silver bullet, but in this case, leaves much to be desired. Is there any explanation for this poor performance?

                  thank-you

                  • 6. Re: Hibernate/Writes but does not loads
                    skajotde

                     

                    "pozzo" wrote:
                    Kamil,

                    It seems that the cache is working, in the log I see 'cache hit'. But I am very disappointed because the performance is equal and in some cases worse than without cache. And the sql queries continue to appear in the console, which seems strange.

                    I know that the cache is not a silver bullet, but in this case, leaves much to be desired. Is there any explanation for this poor performance?

                    thank-you


                    Look out at entity dependencies, if are not lazy that are get every time. What SQL are executed ?

                    • 7. Re: Hibernate/Writes but does not loads
                      pozzo

                      I checked the relationships and seems to be fine.

                      Something that was also noticed is that when I do a query (createQuery) cache is not used, the cache is used just when I execute a search for primary key. I forgot any configuration to work with createQuery? Or cache only works with find by pk?

                      • 8. Re: Hibernate/Writes but does not loads
                        manik

                        Have you enabled the query cache? http://www.hibernate.org/hib_docs/v3/reference/en/html/performance.html#performance-cache - see section 19.4 on the QueryCache.

                        • 9. Re: Hibernate/Writes but does not loads
                          pozzo

                          Hi Manik,

                          I enabled the query cache and worked very well.

                          Thanks