Spring hibernate ehcache setup

匆匆过客 提交于 2019-12-06 07:31:53

Do you need to manually tell Hibernate to use the EHCache provider? I've never really been sure if this is required, but Hibernate does support a number of cache providers so I suspect that it might be necessary to explicitly tell Hibernate which one you want. Try adding this property to ApplicationContext.xml:

<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>

There were several causes that I've identified:

  1. Correct maven dependencies:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    
  2. Added the @Cacheable annotation from javax.persistence to my entities.

  3. Read logging from hibernate instead of ehcache.

    getSessionFactory().getStatistics().logSummary();

  4. Not all hibernate operations seems to affect the cache. This I need to read up on further.

By looking at your config, it all seems fine afaict. Only thing worth noticing, is that when using the HibernateTemplate, you have to explicitly setCacheQueries(true) if you plan on using the query cache... Which I wouldn't recommend, except if you really need it: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html#setCacheQueries(boolean)

Did you try the Hibernate statistics instead of the Ehcache ones ? Do you get cache misses there ? (reason I ask is to be sure you do use the same CacheManager as Hibernate does)...

you can reference following configure

<prop key="hibernate.cache.use_query_cache">true</prop>

In hibernate.cfg.xml add:

 <hibernate-configuration>
   <session-factory>
     ...
     <property name="cache.use_second_level_cache">true</property>
     <property name="cache.use_query_cache">true</property>
     <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!