[英]hibernate second level cache configuration issue
我正在使用Hibernate作為我的ORM。 我需要緩存一個靜態表(常量表)。 我對部署在Tomcat 7上的后端進行了REST調用,但是即使我啟用了二級緩存並將查詢設置為要緩存,也會為每個調用查詢靜態數據。 以下是我的配置。如果我缺少任何東西,有人可以幫助我嗎?
的hibernate.cfg.xml
<property name="hibernate.cache.use_query_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property > name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactor>y</property>
<mapping resource="TableConstants.hbm.xml"/>
<class-cache usage="read-only" class="com.test.hibernateModel.Constants" />
ehcache.xml中
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="1800" />
<cache
name="org.hibernate.cache.internal.StandardQueryCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600">
</cache>
<cache
name="org.hibernate.cache.spi.UpdateTimestampsCache"
maxElementsInMemory="10000"
eternal="false">
</cache>
<cache name="com.test.hibernateModel.Constants"
maxElementsInMemory="500"
eternal="true"
overflowToDisk="false"
/>
以下是我的DAO代碼
currentSess=getSessionFactory().openSession();
Criteria consCrit = currentSess.createCriteria(Constants.class);
consCrit.setCacheable(true);
retList=consCrit.list();
我也在下面的代碼中嘗試過,但它也進行了新的查詢
currentSess=getSessionFactory().openSession();
Query q=currentSess.createQuery("from Constants");
q.setCacheable(true);
return q.list();
我能夠解決。 我正在從Spring加載我的Hibernate Configuration(它從下面的方法加載各種屬性)
Properties hibernateProperties() {
Properties p = new Properties();
p.setProperty("hibernate.show_sql",env.getProperty("hibernate.show_sql"));
//p.setProperty("hibernate.current_session_context_class", "org.hibernate.context.internal.ThreadLocalSessionContext") ;
p.setProperty("hibernate.current_session_context_class", "thread") ;
p.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
p.setProperty("hibernate.cache.use_query_cache",env.getProperty("hibernate.cache.use_query_cache") );
p.setProperty("cache.provider_class",env.getProperty("cache.provider_class") );
p.setProperty("hibernate.cache.use_second_level_cache",env.getProperty("hibernate.cache.use_second_level_cache") );
p.setProperty("hibernate.cache.region.factory_class",env.getProperty("hibernate.cache.region.factory_class") );
return p;
}
我當時使用hibernate.cfg.xml進行POC,卻沒有意識到Spring會以這種方式加載hibernate配置,因此添加了屬性以上述方法啟用二級緩存並能夠使用它。
感謝您的協助,安庫
我的ehcache.xml就是這樣,您可以嘗試這個嗎。
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
properties="jndiName=java:/TransactionManager" propertySeparator=";"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="false"
/>
</ehcache>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.