我是新的休眠和春季,我試驗休眠二級緩存。但它似乎不起作用。我有以下測試類:設置休眠第二級緩存
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration
@Transactional
public class CacheTest extends AbstractTransactionalJUnit4SpringContextTests
{
@Test
public void testCache1()
{
System.out.println("Running testCache1");
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
MutableDAO<AppUser> appUserDAO = new MutableDAOImpl<AppUser>(AppUser.class, (SessionFactory) ctx.getBean("OnMediaSessionFactory"), 10);
assertNotNull("AppUser DAO is null.", appUserDAO);
SessionFactory sessionFactory = (SessionFactory)ctx.getBean("OnMediaSessionFactory");
long numberOfUsers = appUserDAO.countAll();
System.out.println("Number of rows :" + numberOfUsers);
final String cacheRegion = AppUser.class.getCanonicalName();
SecondLevelCacheStatistics settingsStatistics = sessionFactory.getStatistics().
getSecondLevelCacheStatistics(cacheRegion);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
appUserDAO.findAll();
stopWatch.stop();
System.out.println("Query time : " + stopWatch.getTotalTimeSeconds());
System.out.println(settingsStatistics);
}
@Test
public void testCache2()
{
System.out.println("Running testCache2");
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
MutableDAO<AppUser> appUserDAO = new MutableDAOImpl<AppUser>(AppUser.class, (SessionFactory) ctx.getBean("OnMediaSessionFactory"), 10);
assertNotNull("AppUser DAO is null.", appUserDAO);
SessionFactory sessionFactory = (SessionFactory)ctx.getBean("OnMediaSessionFactory");
long numberOfUsers = appUserDAO.countAll();
System.out.println("Number of rows :" + numberOfUsers);
final String cacheRegion = AppUser.class.getCanonicalName();
SecondLevelCacheStatistics settingsStatistics = sessionFactory.getStatistics().
getSecondLevelCacheStatistics(cacheRegion);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
appUserDAO.findAll();
stopWatch.stop();
System.out.println("Query time : " + stopWatch.getTotalTimeSeconds());
System.out.println(settingsStatistics);
}
}
和我有
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
,但我得到的輸出是這樣的:
Running testCache1
Number of rows :81
Query time : 0.129
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=81,elementCountInMemory=81,elementCountOnDisk=0,sizeInMemory=219634]
Running testCache2
Number of rows :81
Query time : 0.063
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=81,elementCountInMemory=81,elementCountOnDisk=0,sizeInMemory=219634]
我有什麼做的就是它的工作?
實體類本身必須作出明確緩存 - 你這樣做呢? – skaffman 2011-03-03 13:51:42
是的,我有@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) – Daniel 2011-03-03 13:55:30