1

I'm using Hibernate 3.3.4.GA. In our hibernate.cfg.xml file, we specify …

    <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>

But executing a test against the DB (from JUnit) results in a miss count of zero and a hit count of zero, which is totally confusing …

@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());
}

Below is the method we use for retrieving results. Any ideas why both miss count and hit count would be zero?

@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;
}

Clearly the cache isn't enabled, but I can't figure out why. - Dave

Dave
  • 8,667
  • 25
  • 72
  • 90

2 Answers2

0

You're using Hibernate 3.3.4, but configuring cache.provider_class which is for pre 3.3 Hibernate.

Check this link for more details: http://ehcache.org/documentation/user-guide/hibernate

shrini1000
  • 7,038
  • 12
  • 59
  • 99
0

Please check my answer here for a working second-level-cache configuration:

Second Level Cache Configuration

I hope it helps.

UPDATE:

Try this:

Statistics stats = sessionFactory.getStatistics();
stats.setStatisticsEnabled(true);
SecondLevelCacheStatistics cacheStats = stats.getSecondLevelCacheStatistics(cacheRegion);
Community
  • 1
  • 1
lepike
  • 726
  • 3
  • 11
  • My configuration is the same (except you disable statistics) and it still isn't working. Why are misses and hits always zero? Shouldn't misses be some number if we weren't hitting the cache? – Dave Jun 28 '11 at 15:37
  • Don't you miss the annotation on the cached entities or the xml fragment in the ehcache.xml? This example nicely works for me. I don't know what can be the problem. – lepike Jun 28 '11 at 16:03
  • I edited my resopnse -- I had the annotations. I think the cache is working but the statistics package isn't -- why else would it return zero for hits and misses always? How do you enable the SecondLevelCacheStatistics? – Dave Jun 28 '11 at 16:48
  • Please try the UPDATE section in my answer. I think this will enable statistics. – lepike Jun 28 '11 at 20:54