我正在使用Hibernate 3.3.4.GA.在我们的hibernate.cfg.xml文件中,我们指定了......
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_configuration">classpath:ehcache.xml</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
但是对DB执行测试(来自JUnit)会导致错过计数为零,命中数为零,这完全令人困惑......
@Test
public void testCache() {
final String cacheRegion = WebLead.class.getCanonicalName();
final SecondLevelCacheStatistics settingsStatistics = sessionFactory.getStatistics().getSecondLevelCacheStatistics(cacheRegion);
// Make first query.
webLeadsDAO.getLeads(lead);
System.out.println("miss count:" + settingsStatistics.getMissCount());
System.out.println("hit count:" + settingsStatistics.getHitCount());
// Make second query, expect this to hit cache.
webLeadsDAO.getLeads(lead);
System.out.println("after second query, hit count: " + settingsStatistics.getHitCount());
}
以下是我们用于检索结果的方法。任何错过计数和命中数的原因都是零?
@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
@Table(name="LEAD")
public WebLeads getLeads(WebLead webLead) {
final Session session = sessionFactory.openSession();
final Criteria crit = session.createCriteria(WebLead.class);
crit.setCacheable(Boolean.TRUE);
// Find webLeads matching input
crit.add( Example.create(webLead) );
// Make special exception for primary key since that isn't covered by Example.create
if (webLead.getLEAD_ID() != null) {
crit.add(Restrictions.eq("LEAD_ID", webLead.getLEAD_ID()));
} // if
@SuppressWarnings("unchecked")
final List<WebLead> results = crit.list();
final WebLeads webLeads = new WebLeads();
webLeads.addAll( results );
session.close();
return webLeads;
}
显然,缓存未启用,但我无法弄清楚原因。 - 戴夫
答案 0 :(得分:0)
请在此处查看我的答案,了解有效的二级缓存配置:
Second Level Cache Configuration
我希望它有所帮助。
<强>更新强>
试试这个:
Statistics stats = sessionFactory.getStatistics();
stats.setStatisticsEnabled(true);
SecondLevelCacheStatistics cacheStats = stats.getSecondLevelCacheStatistics(cacheRegion);
答案 1 :(得分:0)
您正在使用Hibernate 3.3.4,但正在为cache.provider_class
配置pre 3.3 Hibernate
。
点击此链接了解详情:http://ehcache.org/documentation/user-guide/hibernate