`

Spring中二级缓存的配置

 
阅读更多
1.首先,在spring的hibernate配置里(applicationContext-config.xml) 加上如下属性:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">          
  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>org/appfteaching/model/UserVO.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
          <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
          <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
          <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
          <prop key="hibernate.cache.use_query_cache">true</prop>
          <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
   </props>   
  </property>  
 </bean> 

 

 

2.其次,在src目录下的ehcache.xml中配置如下信息(如果是默认ehcache.xml则会有<cache name="sampleCache1">和<cache name="sampleCache2>",去掉)

我的如下配置:

<echcache>
	<diskStore path="java.io.tmpdir"/>
	<!-- 默认缓存 -->
	<defaultCache 
		eternal="false"
		overflowToDisk="true"
		timeToLiveSeconds="120"
		diskPersistent="false"
	/> 

	<!-- 查询缓存 -->
	<cache name="org.hibernate.cache.StandardQueryCache"
	        maxElementsInMemory="10000"
	        eternal="false"
	        timeToIdleSeconds="300"
	        timeToLiveSeconds="4200"
	        overflowToDisk="true">
	</cache>

    <!-- 二级缓存 -->
	<cache  name="org.hibernate.cache.UpdateTimestampsCache"
        maxElementsInMemory="5000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
     /> 
     
	<!-- 给Model设置缓存 -->
	<cache name="pack.java.ssh.mapping.vo.UserVO" 
		maxElementsInMemory="1000"
		eternal="false"
		timeToIdleSeconds="100"
		timeToLiveSeconds="4200"
		overflowToDisk="true"
	/>
</echcache>

  

 

3.最后一步,在UserVO.hbm.xml里加上

<cache usage="read-write"/>

启动Tomcat,如发现如下错误

Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.

 

则是第二步没有做,加上即可.配置完毕

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics