tru"> tru" />
欢迎来到天天文库
浏览记录
ID:40776714
大小:24.04 KB
页数:5页
时间:2019-08-07
《二级缓存的试用》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、二级缓存的试用1.加入ehcase.xml文件; 2.在配置文件中加入如下代码: true org.hibernate.cache.EhCacheProvider zhJava代码 1.pack
2、age com.bjsxt.hibernate; 2. 3.import org.hibernate.CacheMode; 4.import org.hibernate.Session; 5.import org.hibernate.SessionFactory; 6. 7.import junit.framework.TestCase; 8. 9./** 10. * 测试二级缓存 11. * @author Administrator 12. * 13. */ 14.public class
3、CacheLevel2Test extends TestCase { 15. 16. /** 17. * 开启二级缓存,开启两个session,分别调用load方法 18. */ 19. public void testCache1() { 20. Session session = null; 21. try { 22. session = HibernateUtils.getSession(); 23.
4、 session.beginTransaction(); 24. 25. Student student = (Student)session.load(Student.class, 1); 26. System.out.println("student.name=" + student.getName()); 27. session.getTransaction().commit(); 28.
5、 }catch(Exception e) { 29. e.printStackTrace(); 30. session.getTransaction().rollback(); 31. }finally { 32. HibernateUtils.closeSession(session); 33. } 1. 2. try { 3. session =
6、HibernateUtils.getSession(); 4. session.beginTransaction(); 5. 6. //不会发出sql,因为开启了二级缓存,session是共享二级缓存的 7. Student student = (Student)session.load(Student.class, 1); 8. System.out.println("student.nam
7、e=" + student.getName()); 9. session.getTransaction().commit(); 10. }catch(Exception e) { 11. e.printStackTrace(); 12. session.getTransaction().rollback(); 13. }finally { 14. HibernateUtils.close
8、Session(session); 15. } 16. 17. } 18. 19. /** 20. * 开启二级缓存,开启两个session,分别调用get方法 21. */ 22. public void testCache2() { 23. Session session =
此文档下载收益归作者所有