-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate Cache for Scalar selects
PostPosted: Mon Feb 04, 2013 3:21 am 
Newbie

Joined: Mon Feb 04, 2013 2:39 am
Posts: 3
Hi,

Am new to this forum as well as to Hibernate. I am developing a Java web app which gets data from MySQL DB using Hibernate 3.6 in Spring. I am in the process of enabling cache. After reading so many posts I am totally confused. Below is what I want and what I have.

1. My app does not make any changes to DB. It just reads the data. It will not be aware of the changes done to the existing data in DB.
2. All my queries return scalar data. Not whole objects. (Ex: Select employee.name,office.address,MIN(employee.rec) from ... where..)

3. I want my queries to be cached. So I enabled query cache in hibernate configuration.And added
Code:
<query name="QueryName" cacheable="true">
to all my queries in mapping files.

4. I enabled second level cache.
Code:
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>


Now I am at loss. As all my query results are just scalars, how the second level cache works? Adding the below annotation for the persistence class will work?
Code:
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)


5. I want to refresh the cache every 24 hours. Changing the setting in ehcache.xml will suffice?

Reading variety of posts on this topic left me wondering If this is really going to achieve any performance gain..

Please provide me some guidance.


Top
 Profile  
 
 Post subject: Re: Hibernate Cache for Scalar selects
PostPosted: Tue Feb 05, 2013 4:19 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
For caching queries, beside hibernate.cache.use_second_level_cache, you must enable the second-level-query cache,
which you have to enable separately with:
Code:
<prop key="hibernate.cache.use_query_cache">true</prop>

I think that's your problem.

Quote:
Adding the below annotation for the persistence class will work?

Use this when you want to cache whole entity objects, it seems not to be your case.


Top
 Profile  
 
 Post subject: Re: Hibernate Cache for Scalar selects
PostPosted: Wed Feb 06, 2013 1:35 am 
Newbie

Joined: Mon Feb 04, 2013 2:39 am
Posts: 3
Thanks for your response. I have already enabled query cache. If annotating is not required then how second level cache will work for my queries. As of now there is no hit/miss on second level cache(Enabled statistics and logged summary to check this).
I have read somewhere that second level cache will be hit only when there is a query on primary key. My queries are not based on primary key. So how second level cache handles these queries?


Top
 Profile  
 
 Post subject: Re: Hibernate Cache for Scalar selects
PostPosted: Thu Feb 07, 2013 10:43 am 
Newbie

Joined: Mon Feb 04, 2013 2:39 am
Posts: 3
For now I have disabled second level cache as it doesn't seem to work. Having Query cache alone has any side effects?


Top
 Profile  
 
 Post subject: Re: Hibernate Cache for Scalar selects
PostPosted: Mon Feb 11, 2013 9:17 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi djeeva,

in my environment caching scalar query results in 2L-Cache works great (testcase below passes)
Maybe you can compare my result with yours and find out where the problem stays:

My Testcase:
Code:
/**
     * N.B.: For this test-case hibernate.cache.use_second_level_cache can also be set to false
     * @throws PUserException
     */
    public void test2LScalarQueryCache() throws PUserException {
        BartController ctrl = getBartController(getContext());
        CompoundPersonInCharge root = ctrl.getNewCompoundPersonInCharge();
        root.setFunktion("My-Root");
        ctrl.createCompoundPersonInCharge(root);
        addToDelete(root);
        getContext().commit();
        TransactionContext tcnt = (TransactionContext) getContext();
        Session sess = (Session) tcnt._getSession().getEntityManager().getDelegate();
        assertTrue("2L-Query-Caching (hibernate.cache.use_query_cache) must be enabled (check configuration in persistence.xml)!",
                ((SessionFactoryImpl)sess.getSessionFactory()).getSettings().isQueryCacheEnabled());
        sess.getSessionFactory().getStatistics().clear();
     
        PersonInCharge pers = ctrl.lookupPersonInCharge("My-Root");
        assertNotNull(pers);   
       
        Date created = ((AbstractBasePersistentObject) pers).getCreateDate();
        Session session = (Session) ((TransactionContext) getContext())._getSession().getEntityManager().getDelegate();
        SQLQuery query = session.createSQLQuery("select count(*) as n1 from PersonInCharge where createDate = " + created.getDateAsLong());
        IntegerType intt = new IntegerType();
        query.addScalar("n1", intt);
      query.setCacheable(true);
      query.list();
      assertEquals(0, sess.getSessionFactory().getStatistics().getQueryCacheHitCount());
       assertEquals(1, sess.getSessionFactory().getStatistics().getQueryCachePutCount());
       
       
        getContext().commit();
       
        session = (Session) ((TransactionContext) getContext())._getSession().getEntityManager().getDelegate();
        query = session.createSQLQuery("select count(*) as n1 from PersonInCharge where createDate = " + created.getDateAsLong());
        query.addScalar("n1", intt);
      query.setCacheable(true);
      
      int z = (int) query.uniqueResult();
       
        assertEquals(1, sess.getSessionFactory().getStatistics().getQueryCacheHitCount());
        assertEquals(1, sess.getSessionFactory().getStatistics().getQueryCachePutCount());
       
        assertEquals(1, z);
    }


My Logging settings:
log4j.logger.org.hibernate=WARN
log4j.logger.org.hibernate.cache.internal.StandardQueryCache=TRACE

My 2nd-level-cache settings:
Code:
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
    <property name="hibernate.cache.use_second_level_cache" value="false"/>
    <property name="hibernate.cache.use_query_cache" value="true"/>


My OUTPUT :
Code:
...
14:05:52,271 DEBUG StandardQueryCache:130 - Checking cached query results in region: hibernate.test.org.hibernate.cache.internal.StandardQueryCache
14:05:52,271 TRACE StandardQueryCache:214 - key.hashCode=783129251
14:05:52,272 TRACE StandardQueryCache:215 - querySpaces=[]
14:05:52,272 TRACE StandardQueryCache:231 - unexpected returnTypes is typename=integer class=java.lang.Integer ! result
14:05:52,272 DEBUG StandardQueryCache:136 - Query results were not found in cache
p6spy - 1360587952277|4|9|statement
   
    select
        count(*) as n1
    from
        PersonInCharge
    where
        createDate = 20130211140552 
p6spy - 1360587952277|-1||resultset|select count(*)=1
14:05:52,278 DEBUG StandardQueryCache:103 - Caching query results in region: hibernate.test.org.hibernate.cache.internal.StandardQueryCache; timestamp=5572968252526592
14:05:52,278 TRACE StandardQueryCache:214 - key.hashCode=783129251
14:05:52,278 TRACE StandardQueryCache:215 - querySpaces=null
14:05:52,278 TRACE StandardQueryCache:231 - unexpected returnTypes is typename=integer class=java.lang.Integer ! result
14:05:52,279 TRACE StandardQueryCache:265 -  tuple is Object[1]; returnTypes is Type[1]
p6spy - 1360587952281|1|9|commit||
p6spy - 1360587952283|1|9|statement
   
    BEGIN TRAN 'U00001#4_test2LScalarQueryCache' WITH MARK;select @@SPID 
p6spy - 1360587952283|-1||resultset|=123
14:05:52,283 DEBUG StandardQueryCache:130 - Checking cached query results in region: hibernate.test.org.hibernate.cache.internal.StandardQueryCache
14:05:52,283 TRACE StandardQueryCache:214 - key.hashCode=783129251
14:05:52,283 TRACE StandardQueryCache:215 - querySpaces=[]
14:05:52,284 TRACE StandardQueryCache:231 - unexpected returnTypes is typename=integer class=java.lang.Integer ! result
14:05:52,284 DEBUG StandardQueryCache:188 - Checking query spaces are up-to-date: []
14:05:52,284 DEBUG StandardQueryCache:146 -[b] Returning cached query results[/b]
14:05:52,284 TRACE StandardQueryCache:265 -  tuple is Object[1]; returnTypes is Type[1]
p6spy - 1360587952284|0|9|rollback||
p6spy - 1360587952286|0|9|statement
...


Environment: Hibernate Core 4.1.8, EHCACHE_CORE 2.5.1, SQLServer2008 R2


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.