views:

58

answers:

3

Hi All-

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?

+1  A: 

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");
}
becomputer06
+1  A: 

Turn on cache logging.

Michael Wiles
+1  A: 

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.

Pascal Thivent
I don't want to run it in a log - I want to run it in a test page that will print to the screen:Company is cached/not cached
Odelya
@Odelya: Maybe, but you're not the OP :) Just post a more specific question if this one doesn't fulfill your needs.
Pascal Thivent
Hi!I am with Riki in the same team.And I can't change her question:(She wrote the page that needs to check if the objects in the system are cached or not.But we don't want to use log - we want to print it the view (we use JSF)
Odelya