开发者

Hibernate second level cache - print result

I defined a second level cache in my application using @Cache annotation

I am using findById query, as the following:

  开发者_如何转开发long id = 4;    
        Company cmp = companyDAO.findById(id);

Where Company is the object that I get from the DB.

How can I check if the Company object came from the DB or from the cache?


How can I check if the Company object came from the DB or from the cache?

Hibernate uses a specific category to Log all second-level cache activity. The relevant category is org.hibernate.cache, just enable debug for it in the configuration of your logging framework.

See Chapter 3.5 Logging.


Try HitCount and/or MissCount API.

Something like this.....

int oldMissCount = sessionFactory.getStatistics().getSecondLevelCacheStatistics(rName).getMissCount();
int oldHitCount = sessionFactory.getStatistics().getSecondLevelCacheStatistics(rName).getHitCount();

 long id = 4;    
 Company cmp = companyDAO.findById(id);

 int newMissCount = sessionFactory.getStatistics().getSecondLevelCacheStatistics(rName).getMissCount();
 int newHitCount = sessionFactory.getStatistics().getSecondLevelCacheStatistics(rName).getHitCount();
 if(oldHitCount+1 == newHitCount && oldMissCount+1 == newMissCount) {
    logger.debug("came from DB");
   }  else if(oldHitCount+1 == newHitCount && oldMissCount == newMissCount) {
    logger.debug("came from cache");
}


Turn on cache logging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜