在spring中配置hibernate的二级缓…

来源:互联网 发布:网卡mac地址哪个是 编辑:程序博客网 时间:2024/06/07 01:10

1.首先,在spring的hibernate配置里(我的是applicationContext-hibernate.xml) 加上如下属性:

<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        
  <propertyname="dataSource">
   <refbean="dataSource"/>
  </property>
  <propertyname="mappingResources">
   <list>
    <value>org/appfteaching/model/TArticleclass.hbm.xml</value>
   </list>
  </property>
  <propertyname="hibernateProperties">
   <props>
    <propkey="hibernate.dialect">${hibernate.dialect}</prop>
         <propkey="hibernate.show_sql">${hibernate.show_sql}</prop>
         <propkey="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
         <propkey="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
         <propkey="hibernate.cache.use_query_cache">true</prop>
         <propkey="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
   </props>   
  </property>  
 </bean> 

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

<cachename="org.hibernate.cache.StandardQueryCache"
       maxElementsInMemory="10000"
       eternal="false"
       timeToIdleSeconds="300"
       timeToLiveSeconds="4200"
       overflowToDisk="true"
       />

   <!-- Sample cache named sampleCache2
       This cache contains 1000 elements. Elements will always be held inmemory.
       They are not expired. -->

   <cachename="org.hibernate.cache.UpdateTimestampsCache"
       maxElementsInMemory="5000"
       eternal="true"
       timeToIdleSeconds="0"
       timeToLiveSeconds="0"
       overflowToDisk="false"
       />

3.将你要缓存的model加进ehcache.xml里

<cachename="org.appfteaching.model.TArticleclass"
    maxElementsInMemory="1000"
       eternal="false"
       timeToIdleSeconds="100"
       timeToLiveSeconds="4200"
       overflowToDisk="true"
    />  

4.最后一步,在TArticleclass.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.

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

 

转自:http://www.blogjava.net/WshmAndLily/articles/137244.html


                              By:lsfhack Email:lsfhack@163.com QQ:858084865

0 0