http://blog.csdn.net/yerenyuan_pku/article/details/52896195

前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Struts1.3.8了,接下来我们就要为Spring4.2.5集成的Hibernate4.3.11配置二级缓存了,因为在企业开发中,会大量使用到Hibernate的二级缓存。 
由于要在SSH项目中使用到Hibernate4.3.11的二级缓存,所以我们需要向项目中导入Hibernate4.3.11中与二级缓存相关的jar文件,所以要导入如下jar文件: 
 
注意:以上三个jar文件都要导入到项目中,否则会出现各种异常,比如类找不到等等。 
这样,总共需要向SSH项目中导入的jar文件有50个: 
 
接下来我们就来配置Hibernate4.3.11的二级缓存。 
首先我们要修改Spring配置文件中有关Hibernate的配置信息,修改后的内容为:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" /> <!-- 数据源 -->
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value> <!-- Hibernate的实体bean的映射文件(可有多个) -->
</list>
</property>
<!-- hibernateProperties是用来配置Hibernate的属性信息 -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
</value>
</property>
</bean>
  • 1
  • hibernate.cache.use_second_level_cache=true:使用Hibernate4.3.11的二级缓存。
  • hibernate.cache.use_query_cache=false:不使用Hibernate4.3.11的查询缓存,因为一般而言,查询缓存的命中率并不是很高,所以我们没有必要为每个用户的查询缓存他的数据。
  • hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider:指定使用缓存产品的驱动类,在该应用中我们使用了EhCacheProvider这个缓存产品。
  • hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory:该属性是什么意思,我就不知道了,但是得配置,不然就会报错

再接着,我们就要在类路径下新建Ehcache默认的配置文件ehcache.xml,一开始将其内容写为:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="D:\cache" />
<defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="60" />
</ehcache>
  • 1
  • diskStore:指定缓存的对象存放在硬盘上的哪个路径底下。
  • defaultCache:定义缓存的默认行为,即为缺省的缓存策略。 
    • maxElementsInMemory:在缓存中默认存放的元素(对象)个数,即内存中最大允许存在的对象数量。
    • eternal:设置缓存中的对象是否永远不过期,true代表永远不过期,一直存在,false代表要过期了。
    • overflowToDisk:当缓存对象达到内存中最大允许存在的数量时,是否把溢出的对象存放到硬盘上。
    • timeToIdleSeconds:指定缓存对象空闲多长时间就过期,过期的对象会被清除掉。
    • timeToLiveSeconds:指定缓存对象总的存活时间,超过这个值就会被清除掉。
    • diskPersistent:当你的缓存应用关闭的时候,是否需要把缓存的对象持久化到硬盘上,即当JVM结束时是否持久化对象。
    • diskExpiryThreadIntervalSeconds:指定专门用于清除过期对象的监听线程的轮询时间,也就是说后面有一个线程,它会不断扫描,扫描是否有对象过期,有对象过期,就会将其清除掉。

然后在需要缓存的实体bean配置文件中加入缓存配置项,如Person这个实体bean需要使用缓存,可配置一个缓存:

<cache usage="read-write" region="cn.itcast.bean.Person" />
  • usage:缓存策略,这里可以采用读和写。两个并发的事务可对这个对象同时进行读的操作,但是一个事务对它进行写的话,另一个事务是不能够对它进行读的。
  • region:指定缓存的区域名,在区域名里面来存放缓存的对象。区域名可定义为实体类的全称。

这样,Person这个实体bean的映射文件就该为:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<cache usage="read-write" region="cn.itcast.bean.Person" />
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" type="string" length="20" not-null="true" column="name" />
</class>
</hibernate-mapping>
  • 1

当然我们也可以为cn.itcast.bean.Person缓存域来定义一些特殊的缓存设置,如果不定义的话,那么它默认使用的是这种缓存策略:

<defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="60" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

如果cn.itcast.bean.Person缓存域需要一些特别的缓存设置,我们可以为它定义:

<cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="false"/>
  • 1

这样,Ehcache默认的配置文件ehcache.xml的内容就应该是:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="D:\cache" />
<defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="60" />
<cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="false"/>
</ehcache>
  • 1

那怎么检验Person实体bean应用上了缓存呢?我们可以按照这个思路来写代码:如果一旦缓存里面存在了某个id的Person对象之后,第二次再去请求这个相同id的Person对象时,它是不会从数据库里面获取数据的,而是从内存里面获取到缓存对象。依照这个思路我们需要修改PersonServiceTest类中testGetPerson()方法的代码为:

@Test
public void testGetPerson() {
Person person = personService.getPerson(2);
System.out.println(person.getName());
try {
System.out.println("请关闭数据库,即停止MySQL服务");
Thread.sleep(1000*60);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第二次开始获取");
person = personService.getPerson(2);
System.out.println(person.getName());
}

当我们首次测试testGetPerson(),然后迅速停止掉MySQL服务,过约莫1分钟,可发现Eclipse控制台又会打印出id为2的Person对象的name,从而知道第二次是从内存里面获取到缓存对象的。 
如须查看源码,可点击为Spring集成的Hibernate配置二级缓存进行下载。

(转)为Spring集成的Hibernate配置二级缓存的更多相关文章

  1. hibernate配置二级缓存

    ehcache.xml: < ?xml version=”1.0″ encoding=”UTF-8″?>< !– defaultCache节点为缺省的缓存策略 maxElements ...

  2. Hibernate的二级缓存使用(spring使用)

    (一)Hibernate的二级缓存策略的一般过程如下: 1) 条件查询的时候,总是发出一条select * from table_name where …. (选择所有字段)这样的SQL语句查询数据库 ...

  3. Hibernate中"二级缓存"配置

    实体类 : package cn.happy.entity; public class Emp { private Integer empNo; private String empName; pub ...

  4. hibernate(九) 二级缓存和事务级别详讲

    序言 这算是hibernate的最后一篇文章了,下一系列会讲解Struts2的东西,然后说完Struts2,在到Spring,然后在写一个SSH如何整合的案例.之后就会在去讲SSM,在之后我自己的个人 ...

  5. Hibernate4+EhCache配置二级缓存

    本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法 (有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 ) Cache ...

  6. Hibernate之二级缓存

                                                            Hibernate之二级缓存 一.简介 Gaving King曾经对别人说,hibern ...

  7. hibernate的二级缓存

    缓存(Cache): 计算机领域非常通用的概念.它介于应用程序和永久性数据存储源(如硬盘上的文件或者数据库)之间,其作用是降低应用程序直接读写永久性数据存储源的频率,从而提高应用的运行性能.缓存中的数 ...

  8. 不要依赖hibernate的二级缓存

    一.hibernate的二级缓存   如果开启了二级缓存,hibernate在执行任何一次查询的之后,都会把得到的结果集放到缓存中,缓存结构可以看作是一个hash table,key是数据库记录的id ...

  9. SSH整合缓存之-Memcached作为hibernate的二级缓存

    Hibernate本身不提供二级缓存,所以需要使用第三方插件来作为二级缓存:本次使用memcached作为Hiberbate的二级缓存:添加步骤如下: 一.需要安装memcached服务端 1. 下载 ...

随机推荐

  1. Linux 开机引导和启动过程详解

    你是否曾经对操作系统为何能够执行应用程序而感到疑惑?那么本文将为你揭开操作系统引导与启动的面纱. 理解操作系统开机引导和启动过程对于配置操作系统和解决相关启动问题是至关重要的.该文章陈述了 GRUB2 ...

  2. Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法Windows版(手贱强制重启电脑的后果)

    转自:https://blog.csdn.net/rrrrroy_ha/article/details/80601497

  3. 6-9 Haar+adaboost人脸识别

    我们重点分析了Haar特征的概念以及如何计算Haar特征,并介绍了Haar+Adaboost分类器它们的组合以及Adaboost分类器如何使用和训练.这节课我们将通过代码来实现一下Haar+Adabo ...

  4. USACO 奶牛排队

    题目:给出一个只含有1,2,3的数字序列,问最少交换多少次才能将之变为递增数列. 解: 注意到只有1,2,3,我们只要将1,3交换到自己的应在位置上那么排序就已经完成了. 需要交换的有几种,记$a(x ...

  5. PCB 线路铜皮面积(残铜率)计算的实现方法

    一个多月没更新博客园了,这里继续分享关于PCB工程相关一些知识,做过PCB工程都知道用使用genesis或incam是可以非常方便的计算得到铜皮面积这个参数[下图],但实际这个软件是通过什么算法计算出 ...

  6. Swift3.0 元组 (tuples)

    //元组 //不需要的元素用 _ 标记 let (name,age,_) = (","男") print(name,age) //通过下标访问特定的元素 let stud ...

  7. 笔记-JavaWeb学习之旅2

    数据库的基本概念 1.数据库:DataBase 简称 DB,用于存储和管理数据的仓库 特点: 1.持久化存储数据的,其实数据库就是一个文件系统, 2.方便存储和管理数据 3.使用了统一操作数据库 -- ...

  8. SSM报错:No converter found for return value of type: class java.util.ArrayList at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverter

    我使用的是SSM框架,是在编写测试RESTFUL接口的时候出现, @RequestMapping(value = "/selectAll", method = RequestMet ...

  9. 最小公倍数的最小和(Minimum Sum LCM )

    #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> u ...

  10. C++ multiset通过greater、less指定排序方式,实现最大堆、最小堆功能

    STL中的set和multiset基于红黑树实现,默认排序为从小到大. 定义三个multiset实例,进行测试: multiset<int, greater<int>> gre ...