【ERP学习笔记】Hibernate相关配置项

 1 <context:property-placeholder location="WEB-INF/config/database.properties"/>
 2 <property name="hibernateProperties">
 3   <props>
 4     <prop key="hibernate.generate_statistics">true</prop>
 5     <prop key="hibernate.dialect">${hibernate.dialect}</prop>
 6     <prop key="hibernate.show_sql">true</prop>
 7     <prop key="hibernate.format_sql">false</prop>
 8     <prop key="hibernate.use_sql_comments">false</prop>
 9     <prop key="hibernate.query.startup_check">false</prop>
10     <prop key="hibernate.connection.release_mode">after_transaction</prop>
11     <prop key="hibernate.batch_size">50</prop>
12     <!-- config EhCacheProvider -->
13     <prop key="hibernate.cache.use_second_level_cache">true</prop>
14     <prop key="hibernate.cache.use_query_cache">false</prop>
15     <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
16   </props>
17 </property>

 

开头的一些配置都很好理解,至于hibernate的缓存机制,有如下解释

hibernate.cache.use_second_level_cache:是二级缓存,一般需要第三方的支持,比较有名的是Ehcache,此缓存不支持对象
hibernate.cache.use_query_cache:查询缓存,支持对象

同时开启这两个缓存的性能能得到很大的提升

本ERP项目使用的就是EhCache,如最后一行代码提供的类所示

接着,我又在当前目录下找到了ehcache.xml文件,配置如下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!--
 3      defaultCache节点为缺省的缓存策略
 4      maxElementsInMemory 内存中最大允许存在的对象数量
 5      eternal 设置缓存中的对象是否永远不过期
 6      overflowToDisk 把溢出的对象存放到硬盘上
 7      timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉
 8      timeToLiveSeconds 指定缓存对象总的存活时间
 9      diskPersistent 当jvm结束是是否持久化对象
10      diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间
11  -->
12 <ehcache>
13     <diskStore path="/cache"/>
14     <defaultCache  maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
15                    timeToIdleSeconds="120"
16                    timeToLiveSeconds="180"
17                    diskPersistent="false"
18                    diskExpiryThreadIntervalSeconds="60"/>
19 </ehcache>

加上maven中依赖包的配置,没有找到其他关于ehcache的配置代码,我想应该是直接取同目录下的配置文件,这样就将hibernate同ehcache搭配了起来,至于缓存原理我还需要继续研究一番

 

如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用 findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置查询缓存为true

 

从ehcache的配置项来看,缓存原理可以简单被理解为,将查询数据暂存到内存或磁盘中,使得以后的查询都不需要同数据库交互

 

posted @ 2014-04-09 11:29  水漾之舞  阅读(620)  评论(0)    收藏  举报