Hibernate的二级缓存
2016-09-21 10:30
120 查看
</pre>是什么<span style="white-space:pre"> </span>Hibernate的一级缓存是session级别的缓存,这是远远不够的,因此Hibernate提供了二级缓存机制,它是SessionFactory级别的缓存,也就是说当session关闭的时候,下次再查询相关的数据是会从二级缓存读取的,而不是再次发送SQL请求</p><p>怎么办</p><p><span style="white-space:pre"> </span>默认的情况下,Hibernate不会启动二级缓存,需要手动配置</p><p><span style="white-space:pre"> </span>在Hibernate.xfg.xml中启用二级缓存</p><p><span style="white-space:pre"> </span><property name="hibernate.cache.use_second_level_cache">true</property></p><p><span style="white-space:pre"></span><span style="white-space:pre"> </span>1.设置二级缓存的供应商,在这里我们使用EHcache</p><p><span style="white-space:pre"></span></p><pre name="code" class="html"><span style="white-space:pre"> </span><property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
2. 配置类的缓存
在Hibernate.xfg.xml中定义
<class-cache class="全路径名" usage="read-write"/>usage是定义二级缓存的并发策略可取值:
transactional:提供repeatable-read级别的隔离
read-write:提供read-comitted级别的隔离
nostrict-read-write:不锁定缓存中的数据,但是会设置缓存数据在很短的时间内过期
read-only:适用于从来不被修改的数据
注意:不同的缓存供应商所支持的二级缓存的并发策略也不同
3 配置集合的缓存
在Hibernate.xfg.xml中
<collection-cache collection="com.ywkj.Customer.orders" usage="read-write"/>注意:集合缓存是依赖于类缓存的,也就是说上面的定义必须得在配置文件中配置order类的缓存
4 配置EHcache的配置文件
配置EHcache的默认配置文件ehcache.xml,放在类的路径下
二级缓存的一些注意事项
1.二级缓存会将一级缓存的更新进行更新
@Test public void demo3(){ Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Customer customer = (Customer) session.get(Customer.class,1); //二级缓存会同步一级缓存的更新 customer.setName("jyw"); transaction.commit(); session.close(); session = HibernateUtils.openSession(); transaction = session.beginTransaction(); //依旧不会发送SQL语句,因为二级缓存可以随着一级缓存的更新而更新 Customer customer2 = (Customer) session.get(Customer.class,1); transaction.commit(); session.close(); }
2.query的list方法会将查询的数据保存在二级缓存中,但是不会使用二级缓存中的数据
@Test /* * 集合级别的缓存 * 集合级别的缓存依赖于类级别的缓存 * list会将数据放置在二级缓存中,但是不会使用里面的缓存 * */ public void demo2(){ Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); //第一次查询会发送SQL语句 List<Customer> customers = session.createQuery("from Customer").list(); transaction.commit(); session.close(); session = HibernateUtils.openSession(); transaction = session.beginTransaction(); //不会发送SQL语句,会使用List的缓存 Customer customer = (Customer) session.get(Customer.class, 1); //会发送SQL语句 List<Customer> customers2 = session.createQuery("from Customer").list(); transaction.commit(); session.close(); }
3.时间戳区域
Hibernate提供了时间戳缓存区域,用来防止不可重复读的情况发生
@Test /* * 时间戳区域 * */ public void demo4(){ Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); //第一次查询会在集合区域记载一个时间T1 Customer customer = (Customer) session.get(Customer.class,1); //执行更新操作的时候(因为这个时候不是对一级缓存区进行修改,而是直接修改数据库,所以二级缓存区不会被修改) //记载一个时间T2 session.createQuery("update Customer set name = 'jyw' where id = 3").executeUpdate(); transaction.commit(); session.close(); session = HibernateUtils.openSession(); transaction = session.beginTransaction(); //比较T1和T2,发现T2>T1,所以需要强制重新发送SQL语句,而不是仍然使用原有的二级缓存区里的数据 Customer customer2 = (Customer) session.get(Customer.class,1);}
相关文章推荐
- Hibernate缓存:一级缓存和二级缓存
- hibernate一级缓存和二级缓存
- Hibernate3 二级缓存的使用配置细节
- 笔记:Hibernate 二级缓存
- hibernate的二级缓存
- Hibernate 一级缓存(session级别)、二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题
- Hibernate二级缓存
- Java程序员从笨鸟到菜鸟之(七十七)细谈Hibernate(十九)Hibernate二级缓存详解
- 【Hibernate】Hibernate 利用缓存(一级、二级、查询)提高系统性能
- Hibernate二级缓存以及ehcache的搭建配置
- Hibernate中 一 二级缓存及查询缓存的学习总结 转帖
- Hibernate 的二级缓存
- Hibernate使用二级缓存时,createSQLQuery需要注意的问题
- 小用hibernate二级缓存
- hibernate学习笔记(二级缓存)
- Hibernate一级缓存,二级缓存
- hibernate 一级和二级缓存使用总结
- hibernate sessionFactory是做什么的,一级缓存二级缓存都是干什么的
- hibernate一级缓存和二级缓存的区别
- Hibernate二级缓存的配置