-->
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.  [ 1 post ] 
Author Message
 Post subject: testing hibernate cache?
PostPosted: Sun Sep 02, 2007 3:13 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Hi,

Can someone show me how to test the <cache usage="read-write"/> for
hibernate mapping in test cases?

I am using Ehcache with Spring as

Code:
   <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
         <property name="shared" value="true"/>
   </bean>


then a simple test case like

Code:
public class ApplicationResourcePerformance2Test extends AbstractTransactionalDataSourceSpringContextTests {

    protected final Log log = LogFactory.getLog(getClass());
      
    protected String[] getConfigLocations() {
      setAutowireMode(AUTOWIRE_BY_NAME);
      return new String [] {"classpath*:/**/dao/applicationContext-*.xml",
                            "classpath*:META-INF/applicationContext-*.xml"};
    }
   
    private ApplicationResourceDao dao = null;
   
    // injected by the bean cacheManager defined above
    private CacheManager cacheManager;

    public void setApplicationResourceDao(ApplicationResourceDao dao) {
        this.dao = dao;
    }
 
   public void setCacheManager(CacheManager cacheManager) {
      this.cacheManager = cacheManager;
   }
   
    public void testCachePerformance() throws Exception {

          for (int i = 0; i < 1000; i++ ) {
             ApplicationResource appRes = new ApplicationResource(
                   String.valueOf(i), String.valueOf(i), String.valueOf(i));
          }
          
          StopWatch watch = new StopWatch();
          watch.start();
          
         // retrieve all mock data as above
         List appResList = dao.getApplicationResources(null);
          watch.stop();
          
          logger.info( "loading time before caching: " + watch.getTime() );

         printCacheInfo();
         
          watch.reset();
          dao.flush(); 
          
          // print cache info
          printCacheInfo();
          watch.start();
          appResList = dao.getApplicationResources(null);
          watch.stop();
          
          logger.info( "loading time after caching: " + watch.getTime() );

       }
   
    private void printCacheInfo() {
         String names[] = cacheManager.getCacheNames();
         Cache tempCache;
         for (int i = 0; i < names.length; i++) {
            tempCache = cacheManager.getCache(names[i]);
            logger.info("cache name: " + names[i] + ", size: " + tempCache.getSize());
         }
    }

}


the log for printCacheInfo() is like (note the extra lines inserted below)

Quote:
[i18nfuse] INFO [main] SettingsFactory.buildSettings(204) | Order SQL updates by primary key: disabled
[i18nfuse] INFO [main] SettingsFactory.createQueryTranslatorFactory(369) | Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[i18nfuse] INFO [main] ASTQueryTranslatorFactory.<init>(24) | Using ASTQueryTranslatorFactory
[i18nfuse] INFO [main] SettingsFactory.buildSettings(212) | Query language substitutions: {true='Y', false='N'}
[i18nfuse] INFO [main] SettingsFactory.buildSettings(217) | JPA-QL strict compliance: disabled
[i18nfuse] INFO [main] SettingsFactory.buildSettings(222) | Second-level cache: enabled
[i18nfuse] INFO [main] SettingsFactory.buildSettings(226) | Query cache: disabled
[i18nfuse] INFO [main] SettingsFactory.createCacheProvider(356) | Cache provider: org.hibernate.cache.EhCacheProvider
[i18nfuse] INFO [main] SettingsFactory.buildSettings(241) | Optimize cache for minimal puts: disabled
[i18nfuse] INFO [main] SettingsFactory.buildSettings(250) | Structured second-level cache entries: disabled
[i18nfuse] INFO [main] SettingsFactory.buildSettings(277) | Statistics: disabled
[i18nfuse] INFO [main] SettingsFactory.buildSettings(281) | Deleted entity synthetic identifier rollback: disabled
[i18nfuse] INFO [main] SettingsFactory.buildSettings(296) | Default entity-mode: pojo
[i18nfuse] INFO [main] SessionFactoryImpl.<init>(161) | building session factory
[i18nfuse] WARN [main] EhCacheProvider.buildCache(86) | Could not find configuration [org.i18nfuse.model.ApplicationResource]; using defaults.
[i18nfuse] WARN [main] EhCacheProvider.buildCache(86) | Could not find configuration [org.i18nfuse.model.KeyValue]; using defaults.
[i18nfuse] INFO [main] SessionFactoryObjectFactory.addInstance(82) | Not binding factory to JNDI, no JNDI name configured
[i18nfuse] WARN [main] CacheManager.detectAndFixDiskStorePathConflict(302) | Creating a new instance of CacheManager using the diskStorePath "C:\DOCUME~1\samuel\LOCALS~1\Temp\" which is already used by an existing CacheManager.
The source of the configuration was classpath.
The diskStore path for this CacheManager will be set to C:\DOCUME~1\samuel\LOCALS~1\Temp\\ehcache_auto_created_1188645946453.
To avoid this warning consider using the CacheManager factory methods to create a singleton CacheManager or specifying a separate ehcache configuration (ehcache.xml) for each CacheManager instance.
[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.startNewTransaction(309) | Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@19d0e0b]; default rollback = true

[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.testCachePerformance2(45) | loading time before caching: 609

[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.printCacheInfo(62) | # of cache regions: 2
[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.printCacheInfo(66) | cache name: org.i18nfuse.model.ApplicationResource, size: 0
[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.printCacheInfo(66) | cache name: userCache, size: 0

[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.printCacheInfo(62) | # of cache regions: 2
[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.printCacheInfo(66) | cache name: org.i18nfuse.model.ApplicationResource, size: 0
[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.printCacheInfo(66) | cache name: userCache, size: 0

[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.testCachePerformance2(56) | loading time after caching: 0


[i18nfuse] INFO [main] ApplicationResourcePerformance2Test.endTransaction(275) | Rolled back transaction after test execution
[i18nfuse] INFO [Thread-2] SessionFactoryImpl.close(767) | closing


as you can see above, the 2nd level cache is enabled, although loading time seems to have
greatly reduced in loading all ApplicationResource pojo with dao.getApplicationResources(null);
, the log in printCacheInfo() indicates the cache never stored anything!!!

What's the proper way of accessing cache to prove the objects retreived
by getter method have indeed been put in the cache?


Thanks,

Sam


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

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.