Hibernate 二级缓存

8 篇文章 0 订阅
2 篇文章 0 订阅

Hibernate提供了两级缓存,第一级是Session的缓存。由于Session对象的生命周期通常对应一个数据库事务或者一个应用事务,因此它的缓存是事务范围的缓存。第一级缓存是必需的,不允许而且事实上也无法比卸除。在第一级缓存中,持久化类的每个实例都具有唯一的OID。
  第二级缓存是一个可插拔的的缓存插件,它是由SessionFactory负责管理。由于SessionFactory对象的生命周期和应用程序的整个过程对应,因此第二级缓存是进程范围或者集群范围的缓存。这个缓存中存放的对象的松散数据。第二级对象有可能出现并发问题,因此需要采用适当的并发访问策略,该策略为被缓存的数据提供了事务隔离级别。缓存适配器用于把具体的缓存实现软件与Hibernate集成。第二级缓存是可选的,可以在每个类或每个集合的粒度上配置第二级缓存。
  Hibernate的二级缓存策略的过程如下:
  1) 条件查询的时候,总是发出一条select * from table_name where …. (选择所有字段)这样的SQL语句查询数据库,一次获得所有的数据对象。
  2) 把获得的所有数据对象根据ID放入到第二级缓存中。
  3)当Hibernate根据ID访问数据对象的时候,首先从Session一级缓存中查;查不到,如果配置了二级缓存,那么从二级缓存中查;查不到,再查询数据库,把结果按照ID放入到缓存。
  4) 删除、更新、增加数据的时候,同时更新缓存。
  Hibernate的二级缓存策略,是针对于ID查询的缓存策略,对于条件查询则毫无作用。为此,Hibernate提供了针对条件查询的Query缓存。
  Hibernate的Query缓存策略的过程如下:
  1) Hibernate首先根据这些信息组成一个Query Key,Query Key包括条件查询的请求一般信息:SQL, SQL需要的参数,记录范围(起始位置rowStart,最大记录个数maxRows),等。
  2) Hibernate根据这个Query Key到Query缓存中查找对应的结果列表。如果存在,那么返回这个结果列表;如果不存在,查询数据库,获取结果列表,把整个结果列表根据Query Key放入到Query缓存中。
  3) Query Key中的SQL涉及到一些表名,如果这些表的任何数据发生修改、删除、增加等操作,这些相关的Query Key都要从缓存中清空。

使用二级缓存的步骤

1、hibernate并没有提供相应的二级缓存的组件,所以需要加入额外的二级缓存包,常用的二级缓存包是ECHcache

		hibernate-release-4.1.4.Final\lib\optional\ehcache
		ehcache-core-2.4.3.jar
		hibernate-ehcache-4.1.4.Final.jar
		slf4j-api-1.6.1.jar
2、在hibernate.cfg.xml中配置开启二级缓存
<!-- 开启二级缓存 -->
		<property name="hibernate.cache.use_second_level_cache">true</property>
		<!-- 配置二级缓存的工厂类 -->
		<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
  <!-- 在hibernate4.0之后需要设置facotory_class -->
3、设置相应的ehcache.xml文件,在这个文件中配置二级缓存的参数,并且将文件在cfg文件中配置
<!-- 说明ehcache的配置文件路径 -->

    <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    <diskStore path="java.io.tmpdir"/> 配置二级缓存硬盘临时目录位置 
		 <defaultCache  
            maxElementsInMemory="10000" // 内存中最大对象数量 ,超过数量,数据会被缓存到硬盘 
            eternal="false"
            timeToIdleSeconds="120" // 是否缓存为永久性 false 不永久
            timeToLiveSeconds="120" // 存活时间,对象不管是否使用,到了时间回收
            overflowToDisk="true" // 是否可以缓存到硬盘
            maxElementsOnDisk="10000000" // 硬盘缓存最大对象数量 
            // 当jvm结束时是否持久化对象 true false 默认是false
            diskExpiryThreadIntervalSeconds="120"  // 指定专门用于清除过期对象的监听线程的轮询时间 
            memoryStoreEvictionPolicy="LRU" 
            />
</ehcache>
4、开启二级缓存
    在xml的配置中设置
   
<class name="Student" table="t_stu">
        <cache usage="read-only"/>
对于要进行二级缓存的实体类,进行配置,增加

transactional(事务型)
仅在受管理的环境中适用
提供Repeatable Read事务隔离级别
适用经常被读,很少修改的数据
可以防止脏读和不可重复读的并发问题
缓存支持事务,发生异常的时候,缓存也能够回滚
read-write(读写型)
提供Read Committed事务隔离级别
在非集群的环境中适用
适用经常被读,很少修改的数据
可以防止脏读
更新缓存的时候会锁定缓存中的数据
nonstrict-read-write(非严格读写型)
适用极少被修改,偶尔允许脏读的数据(两个事务同时修改数据的情况很少见)
不保证缓存和数据库中数据的一致性
为缓存数据设置很短的过期时间,从而尽量避免脏读
不锁定缓存中的数据
read-only(只读型)
适用从来不会被修改的数据(如参考数据)
在此模式下,如果对数据进行更新操作,会有异常
事务隔离级别低,并发性能高
在集群环境中也能完美运作

<!--配置哪些实体类的对象需要二级缓存  -->
<class-cache usage="read-only" class="com.xxy.entity.Person"/>
5、二级缓存缓存的是对象,它是把所有的对象缓存到内存中,一定注意是基于对象的缓存

6、查询缓存是针对HQL语句的缓存,查询缓存仅仅只会缓存id而不会缓存对象

7、默认情况下, 设置的缓存对 HQL 及 QBC 查询时无效的, 但可以通过以下方式使其是有效的
a.在 hibernate 配置文件中声明开启查询缓存
b.调用 Query 或 Criteria 的 setCacheable(true) 方法

<property name="cache.use_query_cache">true</property>

c. 查询缓存依赖于二级缓存

	@Test
	public void test01() {
		Session session = HibernateUtil.openSession();
		Person person = (Person) session.get(Person.class, 21);
		System.out.println("----0---"+person);
		session.clear();
		// 当使用get查询某个对象时,先去session中(一级缓存)查找。如果有就直接获取,不再查询数据库
		Person p1 = (Person) session.get(Person.class, 21);
		System.out.println("----1---"+p1);
		SessionFactory factory = HibernateUtil.SESSION_FACTORY;
		Statistics st = factory.getStatistics();
		// 打印全部统计信息
		System.out.println("----2---"+st);
		// 打印二级缓存信息
		System.out.println("----3---"+st.getSecondLevelCacheHitCount());
	}

测试结果

Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.status as status0_0_ from t_person person0_ where person0_.id=?
----0---Person [id=21, name=王五, status=11]
----1---Person [id=21, name=王五, status=11]
----2---Statistics[start time=1531102930016,sessions opened=0,sessions closed=0,transactions=0,successful transactions=0,optimistic lock failures=0,flushes=0,connections obtained=0,statements prepared=0,statements closed=0,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=0,entities updated=0,entities inserted=0,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,naturalId queries executed to database=0,naturalId cache puts=0,naturalId cache hits=0,naturalId cache misses=0,naturalId max query time=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,update timestamps cache puts=0,update timestamps cache hits=0,update timestamps cache misses=0,max query time=0]
----3---0

可见开启缓存之后第二次查询时并没有打印sql

下面我们关闭二级缓存

<property name="show_sql">true</property>
		<!-- 开启二级缓存 -->
		<property name="hibernate.cache.use_second_level_cache">true</property>
		<!-- 配置二级缓存的工厂类 -->
		<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
		<mapping class="com.xxy.entity.Person"/>
		<!--配置哪些实体类的对象需要二级缓存
		<class-cache usage="read-only" class="com.xxy.entity.Person"/>  -->

执行结果:

Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.status as status0_0_ from t_person person0_ where person0_.id=?
----0---Person [id=21, name=王五, status=11]
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.status as status0_0_ from t_person person0_ where person0_.id=?
----1---Person [id=21, name=王五, status=11]
----2---Statistics[start time=1531103064709,sessions opened=0,sessions closed=0,transactions=0,successful transactions=0,optimistic lock failures=0,flushes=0,connections obtained=0,statements prepared=0,statements closed=0,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=0,entities updated=0,entities inserted=0,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,naturalId queries executed to database=0,naturalId cache puts=0,naturalId cache hits=0,naturalId cache misses=0,naturalId max query time=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,update timestamps cache puts=0,update timestamps cache hits=0,update timestamps cache misses=0,max query time=0]
----3---0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值