Search code examples
hibernatespringweb-applicationsehcachesecond-level-cache

How to disable ehcache in persistence.xml file


All, is it possible to disable secondary cache in my persistence xml file? I have a Spring+Hibernate+JPA configuration that uses ehcache. In my persistence.xml file I have this entry:

<property name="hibernate.cache.use_second_level_cache" value="false"/>

but this does not seem to work, and I still see the number of entities loaded keeps on increasing, as my application runs. I fetch the statistics using this:

EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
EntityManagerFactory emf = emfi.getNativeEntityManagerFactory();
EntityManagerFactoryImpl empImpl = (EntityManagerFactoryImpl)emf;
log.debug(empImpl.getSessionFactory().getStatistics());

Please help.


Solution

  • The statistics you are looking at is the Hibernate session cache (the 1st level cache). Your 2nd level cache (ehcache) is disabled. What you experience is the normal behaviour.

    EDIT:

    When ehcache is enabled you would find log entries like:

    [#|...|INFO|sun-appserver2.1|org.hibernate.cfg.SettingsFactory|...|Second-level cache: enabled|#]
    [#|...|INFO|sun-appserver2.1|org.hibernate.cfg.SettingsFactory|...|Cache provider: org.hibernate.cache.EhCacheProvider|#]
    

    (provided you enable logging for the package - the cache provider may vary e.g. maybe net.sf.ehcache.hibernate.EhCacheRegionFactory - don't know what you use)