使用 Spring 休眠二级缓存 [英] Hibernate second level cache with Spring
问题描述
我正在使用 Spring + JPA + Hibernate.我正在尝试启用 Hibernate 的二级缓存.在我的 Spring 的 applicationContext.xml
中,我有:
I'm using Spring + JPA + Hibernate. I'm trying to enable Hibernate's second level cache. In my Spring's applicationContext.xml
I have:
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
运行时出现错误:
Caused by: org.hibernate.HibernateException: Could not instantiate cache implementation
at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:64)
Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
at org.hibernate.cache.NoCacheProvider.buildCache(NoCacheProvider.java:21)
所以抱怨我没有启用二级缓存.我尝试通过添加到我的 applicationContext.xml
来启用它:
So it's complain I don't have second level cache enabled. I attempt to enable it by adding to my applicationContext.xml
:
<prop key="hibernate.cache.use_second_level_cache">true</prop>
但仍然没有快乐.我也尝试将它添加到我的 ehcache.xml:
But still no joy. I also tried adding this to my ehcache.xml:
<property name="hibernate.cache.use_second_level_cache">true</property>
但是还是不行.将 provider_class
更改为 org.hibernate.cache.EhCacheProvider
也无济于事:
But it still doesn't work. Changing the provider_class
to org.hibernate.cache.EhCacheProvider
doesn't help either:
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
我的实体类被注释为使用缓存
My entity classes are annotated to use caching
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
那么,如何启用二级缓存?
So, how do I enable second level cache?
这是在bean下:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
已解决:由于我使用的是 LocalEntityManagerFactoryBean
,它从 META-INF/persistence.xml
获取其设置.我在 applicationContext.xml
中的设置甚至没有被读取.
Resolved: Since I'm using LocalEntityManagerFactoryBean
it gets its settings from META-INF/persistence.xml
. My settings in applicationContext.xml
weren't even being read.
推荐答案
我没有回答这个问题,但发帖人自己找到了答案并不明显.我正在转发他的回答:
I didn't answer this, but it's not obvious that the poster found the answer himself. I'm reposting his answer:
由于我使用的是 LocalEntityManagerFactoryBean
,它从 META-INF/persistence.xml
获取其设置.我在 applicationContext.xml
中的设置甚至没有被读取.
Since I'm using LocalEntityManagerFactoryBean
it gets its settings from META-INF/persistence.xml
. My settings in applicationContext.xml
weren't even being read.
这篇关于使用 Spring 休眠二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!