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