1

I'm using Spring 3.1.1.RELEASE, Hibernate 4.1.5.Final, and JUnit 4.8.1. I want to test if my second-level cache is being properly used, but unsure if I'm doing it right. The following JUnit code fails on the last assertion …

private Statistics m_stats;

@Before
public void setup()
{
    m_stats = ((org.hibernate.internal.SessionImpl) m_entityManager.getDelegate()).getSessionFactory().getStatistics();
}   // setup

@Test
public void testGenerateStats()
{
    final String countryId = m_testProps.getProperty("test.country.id");
    final Country country = m_countryDao.findById(countryId);
    Assert.assertNotNull(country);

    final Country country2 = m_countryDao.findById(countryId);
    m_countryDao.findById(countryId);
    Assert.assertTrue(m_stats.getSecondLevelCacheHitCount() > 0);
}

Here is how I configured my domain object …

@Entity
@Table(name = "cb_country")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Country implements Serializable
{
    …
}

and here's my DAO …

@Override
public Country findById(String id)
{
    Country ret = null;
    if (id != null)
    {
        ret = entityManager.find(Country.class, id);
    }   // if
    return ret;
}

Here's how I configured my Hibernate second level cache …

    <!--  Caching -->
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> 
    <!--  Collect stats, this is for testing if the cache is working -->
    <property name="hibernate.generate_statistics">true</property>

Any ideas why my second level cache is not indicating any hits? Thanks - Dave

Dave
  • 15,639
  • 133
  • 442
  • 830
  • 1st comment: there's a better way to get the `Statistics` object from the `EntityManager`, one that doesn't involve casting to Hibernate internal objects. See my code in your other question: http://stackoverflow.com/questions/14106575/how-do-i-access-hibernate-statistics-from-an-entitymanager – Marcel Stör Jan 02 '13 at 13:03
  • 1
    2nd comment: does the hit count you see correspond to the debug messages from `org.hibernate.stat.*` i.e. does Hibernate (or possibly Ehcache) debug messages reveal why there are no cache hits? Sometimes there _are_ cache hits but they're considered expired. – Marcel Stör Jan 02 '13 at 13:05
  • I don't see any debug messages with "org.hibernate.stat." What I'm asking is why isn't a second-level cache hit generated? I get the same entity twice. – Dave Jan 02 '13 at 14:44
  • I guess I didn't express myself clearly, sorry. I do understand your question but I don't have a solution right now. Enabling debug logging might give hints as to why it doesn't work you want to. While developing we always set (for log4j) to understand what's being cached - or not. – Marcel Stör Jan 02 '13 at 15:12

2 Answers2

2

SecondLevelCacheHitCount equals zero because during session (as in your code) hibernate use first level cache and load entities from it. Second level cache wasn't used, so secondLevelCacheHitCount didn't changed.

VMN
  • 275
  • 2
  • 13
0

A bit old now, but if you set your logging up to show hibernate statistics like Marcel Stör says - e.g.

log4j.logger.org.hibernate.stat=TRACE

watch for something like this

DEBUG 2013-10-04 11:09:20,456 [main] org.hibernate.stat.internal.StatisticsInitiator: Statistics initialized [enabled=false]

I see you have hibernate.generate_statistics=true

However, to enable statistics just in my test I used:

m_stats.setStatisticsEnabled(true);

I have a test that I think demonstrates this was enabled by default in version 4.1.7, but was changed sometime after that (I upgraded to 4.1.12, and my caching tests failed), so I'm not sure why it isn't enabled for you in version 4.1.5, but get the stats logging working to know for sure.

Community
  • 1
  • 1
djb
  • 668
  • 6
  • 12