dj8485
dj8485

Reputation: 31

Hibernate cache for scalar queries

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.

Upvotes: 0

Views: 654

Answers (1)

ogzd
ogzd

Reputation: 5692

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()

Upvotes: 1

Related Questions