您的位置:首页 > 其它

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);}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: