您的位置:首页 > 编程语言 > Java开发

spring学习笔记16--Spring2.x+Hibernate3.x +Struts1.x整合开发(4)-----Hibernate3.x配置二级缓存

2013-05-15 23:18 603 查看
一、导入hibernate二级缓存的所用的jar包------------------ehcache-1.2.3.jar

二、在spring配置文件中配置Hibernate的二级缓存相关属性(红色为精华部分)

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<!-- 配置数据源 -->
<bean id="dataSourceOrcl" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
<property name="username" value="websb"/>
<property name="password" value="ddit"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大连接数 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,直到减少到MaxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值,当空闲的连接数少于两个值时,连接池就会预申请一些连接数,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!--hibernate二级缓存的配置  只存在一个(单例) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceOrcl"/>
<!-- hibernate映射源数据 可以多个-->
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.OracleDialect<!--方言  -->
hibernate.hbm2ddl.auto=update <!--  是否根据映射源文件生成表结构 -->
hibernate.show_sql=false <!-- 是否打印sql -->
hibernate.format_sql=false<!-- 是否格式化sql -->
hibernate.cache.use_second_level_cache=true <!--设置可用的二级缓存  -->
hibernate.cache.use_query_cache=false<!-- 设置为不可用的查询缓存:sql的数据利用率不高,所以不用使用缓存 -->
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider<!--指定缓存的驱动类,当然还有很多歌  -->
</value>
</property>
</bean>
<!-- 配置事务管理 :spring提供的hibernate事务管理器:要使用ehcache1.2.3.jar -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!--只要是通过sessionFactory创建的事务都会纳入这个管理器进行管理  -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 采用@Transaction注解方式使用事务
transaction-manager:指定事务管理器
-->
<tx:annotation-driven transaction-manager="txManager"/>
<context:annotation-config/>
<bean id="personService" class="cn.itcast.service.imp.PersonServiceBean"></bean>
<!--将struts中的action交给spring,以至于实现依赖注入  -->
<bean name="/person/list" class="cn.itcast.web.action.PersonSpringAction"></bean>
</beans>


三、在类的路径下配置二级缓存的配置文件-------ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
diskStore:指定缓存的对象存放在硬盘的位置
defaultCache节点为缺省的缓存策略
maxElementsInMemory 内存中最大允许存在的对象数量
eternal 设置缓存中的对象是否永远不过期
overflowToDisk 把溢出的对象存放到硬盘上
timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉
timeToLiveSeconds 指定缓存对象总的存活时间
diskPersistent 当jvm结束是是否持久化对象
diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间
-->
<ehcache>
<diskStore path="D:\cache"/>
<defaultCache  maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="60"/>
<!-- 对person设置单独的缓存策略,如果不定义,则使用上面的默认策略 -->
<cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>
</ehcache>

四、在想要使用二级缓存的映射xml文件上配置缓存策略

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<!-- 配置需要二级缓存的类 -->
<!--
usage:缓存的策略
region:缓存的区域名,Hibernate为每个实体类创建一个Region作为缓存区, 默认情况下使用类的全路径名做为这个Region的名称
-->
<cache usage="read-write" region="cn.itcast.bean.Person"/>
<!-- 对象标示符,类型可以不写,hibernate自己识别 -->
<id name="id">
<!-- 指定主键生成方式。
native根据方言判定生成主键的方式
-->
<generator class="native"/>
<!--  <generator class="uuid.hex"/>-->
<!-- <generator class="sequence">
<param name="sequence">PERSONID_SEQ</param>
</generator>-->

</id>
<!-- 设置属性 -->
<property name="name" length="10" not-null="true" />
</class>

</hibernate-mapping>

五、测试类

由于二级缓存的特殊性

描述下测试过程:

(1)调用getPerson(id),数据就会放到缓存中

(2)关闭数据库连接

(3)再次调用getperson(同一id)。从缓存中读取数据

/**
* 测试hibernate二级缓存
*/
@Test
public void testGetCachePerson() {
Person person=persionService.getPerson(7);
System.out.println(person.getName());

//睡眠60秒,之间关闭数据库
try {
System.out.println("看什么看赶紧关闭数据库!");
Thread.sleep(1000*60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("第二次获取数据");
person=persionService.getPerson(7);
System.out.println(person.getName());
}


剩下的就是:翻滚日志,跑起来…………………………
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: