spring结合hibernate cache配置(转)

一、 applicationContext.xml
< bean id ="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
       
< property name ="hibernateProperties" >
           
< props >
                ......
               
< prop key ="hibernate.cache.provider_class" >
                    org.hibernate.cache.EhCacheProvider
               
</ prop >
               
<!-- 若是用查询缓存,就去掉注释
                <prop key="hibernate.cache.use_query_cache">true</prop>
               
-->
           
</ props >
       
</ property >
        ......
   
</ bean >
二、 在classpath下(通常都是src)下创建ehcache.xml文件,这个文件在hibernate-x.x/etc的目录下有个样板,拷贝过来便可

3 、里面的defaultCache是默认的缓存配置,本身能够增长制定实体的缓存配置,例如
<cache name="xxx.Entity1" maxElementsInMemory="500" eternal="false" timeToLiveSeconds="7200" timeToIdleSeconds="3600" overflowToDisk="true" />
建议自定义cache时,cache名字和类路径名相同。
(1)不要使用默认缓存策略defaultCache(多个class共享)
(2)不要给cache name另外起名
不然继承AbstractTransactionalDataSourceSpringContextTests作测试时,抛出
org.hibernate.cache.CacheException: java.lang.IllegalStateException: The com.sitechasia.occ.core.base.ExampleForTest Cache is not alive
四、 pojo与ehcache.xml的配置关系
以com.gjun.vo.Person为例子
在*.hbm.xml中配置:
<class name="com.gjun.vo.Person" table="perosn" lazy="false">
<cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句须要紧跟在class标签下面,其余位置无效。
ehcache.xml文件主体以下
<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
<cache
        name="com.gjun.vo.Person"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
/>
hbm文件查找cache方法名的策略:若是不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.gjun.vo.Person的cache,若是不存在与类名匹配的cache名称,则用defaultCache。
若是Person包含set集合,则须要另行指定其cache
例如Person包含addresses Set集合,则须要
添加以下配置到ehcache.xml中
<cache
       name="com.gjun.vo.Address"
       maxElementsInMemory="10000"
       eternal="false" timeToIdleSeconds="300"
       timeToLiveSeconds="600"
       overflowToDisk="true"
/>
另,针对查询缓存的配置以下:
<cache
         name="org.hibernate.cache.UpdateTimestampsCache"
         maxElementsInMemory="5000"
         eternal="true"
         overflowToDisk="true"
/>
<cache
       name="org.hibernate.cache.StandardQueryCache"
       maxElementsInMemory="10000"
       eternal="false"
       timeToLiveSeconds="120"
       overflowToDisk="true"
/>
类级缓存,配置完了自动就用了

查询缓存须要激活一下
query.setCacheable(true);//激活查询缓存
query.setCacheRegion("xxx");
//你的查询语句,当你load、get、list、iterator时,都会填充

EHCache.xml要作相应配置,若是不作配置那么就执行默认的缓存配置StandardQueryCache ,这个你也会,确定能明白
<cache name="xxx" maxElementsInMemory="50" eternal="false" timeToIdleSeconds="3600"
timeToLiveSeconds="7200" overflowToDisk="true"/>
 
五、 选择缓存策略依据:
<cache
usage="transactional|read-write|nonstrict-read-write|read-only" (1)
/>
ehcache不支持transactional,其余三种能够支持。
read-only:无需修改, 那么就能够对其进行 只读 缓存,注意,在此策略下,若是直接修改数据库,即便可以看到前台显示效果,可是将对象修改至cache中会报error,cache不会发生做用。另:删除记录会报错,由于不能在read-only模式的对象从cache中删除。
read-write:须要更新数据,那么使用 读/写缓存 比较合适,前提:数据库不能够为serializable transaction isolation level(序列化事务隔离级别)
nonstrict-read-write:只偶尔须要更新数据(也就是说,两个事务同时更新同一记录的状况很不常见),也不须要十分严格的事务隔离,那么比较适合使用 非严格读/写缓存策略。
 
六、 调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操做过程,主要用于调试过程,实际应用发布时候,请注释掉,以避免影响性能。
 
七、 使用ehcache,打印sql语句是正常的,由于query cache设置为true将会建立两个缓存区域:一个用于保存查询结果集(org.hibernate.cache.StandardQueryCache); 另外一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。须要将打印sql语句与最近的cache内容相比较,将不一样之处修改到cache中,因此查询缓存一般会和二级缓存一块儿使用。
相关文章
相关标签/搜索