0

I have read this post. I cannot get whole objects as suggested in this post. I am getting MIN,MAX,Count values. So a new post. Below is what I want and what I have.

  1. My Spring app does not make any changes to DB(MySQL). 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 cacheable="true" to my named queries.

4.I enabled second level cache.

5.Added the below annotation for the persistence class.

@Cacheable 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

6. I want to refresh the cache every 24 hours. So updated timeToLiveSeconds in ehcache.xml defaultCache.

Is this the correct approach? How to know whether the query hits second level cache from query cache?

UPDATE: I added statistics and there was no hit/miss on second level cache.

Community
  • 1
  • 1
dj8485
  • 31
  • 6
  • There's no point in making the entities cacheable, since you never return them from your queries. To know if the cache is hit, turn on SQL logging (by setting the propert hibernate.show_sql to true), and see if queries are generated or not. – JB Nizet Feb 04 '13 at 07:58
  • Thanks @JBNizet. I assume all my other configurations are correct :) – dj8485 Feb 04 '13 at 09:47

1 Answers1

1

Turn on SQL logging and statistics generation. If you see that SQL queries logging, that means cache doesn't work.

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

In order to see the hit count on second level cache:

session.getSessionFactory().getStatistics().getSecondLevelCacheHitCount()
ogzd
  • 5,532
  • 2
  • 26
  • 27
  • thanks for your response. Regarding scalar queries, does this have any impact on second level cache? – dj8485 Feb 04 '13 at 09:45
  • you might want to take a look at this: http://stackoverflow.com/questions/8480268/how-query-cache-works-for-scalar-results – ogzd Feb 04 '13 at 10:09
  • I do not use iterate(). And I cannot get the whole object as I need to use MAX() in the query. I added the above mentioned configurations. No hit on second level cache. – dj8485 Feb 04 '13 at 10:25