hibernate二级缓存

hibernate 二级缓存:(缓存的是实体对象,二级缓存是放变化不是很大的数据)








二级缓存也称进程级的缓存或SessionFactory级的缓存,而二级缓存可以被所有的session(hibernate中的)共享二级缓存的生命周期和SessionFactory的生命周期一致,SessionFactory可以管理二级缓存

 

二级缓存的配置和使用:

1.将echcache.xml文件拷贝到src下, 二级缓存hibernate默认是开启的,手动开启

2.开启二级缓存,修改hibernate.cfg.xml文件,

<property name=”hibernate.cache.user_second_level_cache”>true</property>

3.指定缓存产品提供商

<property name=”hibernate.cache.provider_calss”>org.hibernate.cache.EhCacheProvider</property>

 

4.指定那些实体类使用二级缓存(两种方法,推荐使用第二种)

第一种:在*.hbm.xml中,在<id>之前加入

<cache usage=”read-only” />, 使用二级缓存

第二种:在hibernate.cfg.xml配置文件中,在<mapping resource=”com/Studnet.hbm.xml” />后面加上:

<class-cache class=” com.Studnet” usage=”read-only” /> 

 

二级缓存是缓存实体对象的

了解一级缓存和二级缓存的交互

 

 

 

 

测试二级缓存:

一.开启两个session中发出两次load查询(getload一样,同样不会查询数据库),

Student sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

sessioin.close();

………..

       sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

开启两个session中发出两次load查询,第一次load的时候不会去查询数据库,因为他是LAZY的,当使用的时候才去查询数据库,  第二次load的时候也不会,当使用的时候查询数据库,开启了二级缓存,也不会查询数据库。

 

 

二.开启两个session,分别调用load,再使用sessionFactory清楚二级缓存

Student sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

sessioin.close();

………..

SessionFactory factory = HibernateUtil.getSessionFactory();

//factory.evict(Student.class); //清除所有Student对象

Factory.evict(Student.class,1); //清除指定id=1 的对象

 

       sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

开启两个session中发出两次load查询,第一次load的时候不会去查询数据库,因为他是LAZY的,当使用的时候才去查询数据库,  第二次load的时候也不会,当使用的时候查询数据库,它要查询数据库,因为二级缓存中被清除了

 

三.一级缓存和二级缓存的交互

 

session.setCacheMode(CacheMode.GET);    //设置成 只是从二级缓存里读,不向二级缓存里写数据

Student sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

sessioin.close();

………..

SessionFactory factory = HibernateUtil.getSessionFactory();

//factory.evict(Student.class); //清除所有Student对象

Factory.evict(Student.class,1); //清除指定id=1 的对象

 

       sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

开启两个session中发出两次load查询,第一次load的时候不会去查询数据库,因为他是LAZY的,当使用的时候才去查询数据库,  第二次load的时候也不会,当使用的时候查询数据库,它要查询数据库,因为 设置了CacheMode为GET,(load设置成不能往二级缓冲中写数据), 所以二级缓冲中没有数据

 

 

 

session.setCacheMode(CacheMode.PUT);  //设置成只是向二级缓存里写数据,不读数据

Student sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

sessioin.close();

………..

SessionFactory factory = HibernateUtil.getSessionFactory();

//factory.evict(Student.class); //清除所有Student对象

Factory.evict(Student.class,1); //清除指定id=1 的对象

 

       sutdent = (Student)session.load(Student.class,1);

       System.out.println(student.getName());

开启两个session中发出两次load查询,第一次load的时候不会去查询数据库,因为他是LAZY的,当使用的时候才去查询数据库,  第二次load的时候也不会,当使用的时候查询数据库,它要查询数据库,因为设置了CacheMode为POST,(load设置成只是向二级缓存里写数据,不读数据)

 

posted on 2009-04-06 12:02 胡鹏 阅读(789) 评论(0)  编辑  收藏 所属分类: hibernate


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2009年4月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

统计

常用链接

留言簿(3)

随笔分类

随笔档案

agile

搜索

最新评论

阅读排行榜

评论排行榜