



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
二级缓存的试用1.加入ehcase.xml文件;2.在配置文件中加入如下代码:true org.hibernate.cache.EhCacheProviderzhJava代码1. packagecom.bjsxt.hibernate;2. 3. importorg.hibernate.CacheMode;4. importorg.hibernate.Session;5. importorg.hibernate.SessionFactory;6. 7. importjunit.framework.TestCase;8. 9. /*10. *测试二级缓存11. *authorAdministrator12. *13. */14. publicclassCacheLevel2TestextendsTestCase15. 16. /*17. *开启二级缓存,开启两个session,分别调用load方法18. */19. publicvoidtestCache1()20. Sessionsession=null;21. try22. session=HibernateUtils.getSession();23. session.beginTransaction();24. 25. Studentstudent=(Student)session.load(Student.class,1);26. System.out.println(=+student.getName();27. session.getTransaction().commit();28. catch(Exceptione)29. e.printStackTrace();30. session.getTransaction().rollback();31. finally32. HibernateUtils.closeSession(session);33. 34. 35. try36. session=HibernateUtils.getSession();37. session.beginTransaction();38. 39. /不会发出sql,因为开启了二级缓存,session是共享二级缓存的40. Studentstudent=(Student)session.load(Student.class,1);41. System.out.println(=+student.getName();42. session.getTransaction().commit();43. catch(Exceptione)44. e.printStackTrace();45. session.getTransaction().rollback();46. finally47. HibernateUtils.closeSession(session);48. 49. 50. 51. 52. /*53. *开启二级缓存,开启两个session,分别调用get方法54. */55. publicvoidtestCache2()56. Sessionsession=null;57. try58. session=HibernateUtils.getSession();59. session.beginTransaction();60. 61. Studentstudent=(Student)session.get(Student.class,1);62. System.out.println(=+student.getName();63. session.getTransaction().commit();64. catch(Exceptione)65. e.printStackTrace();66. session.getTransaction().rollback();67. finally68. HibernateUtils.closeSession(session);69. 70. 71. try72. session=HibernateUtils.getSession();73. session.beginTransaction();74. 75. /不会发出sql,因为开启了二级缓存,session是共享二级缓存的76. Studentstudent=(Student)session.get(Student.class,1);77. System.out.println(=+student.getName();78. session.getTransaction().commit();79. catch(Exceptione)80. e.printStackTrace();81. session.getTransaction().rollback();82. finally83. HibernateUtils.closeSession(session);84. 85. 86. 87. 88. /*89. *开启二级缓存,先调用load,再使用SessionFactory清除二级缓存90. */91. publicvoidtestCache3()92. Sessionsession=null;93. try94. session=HibernateUtils.getSession();95. session.beginTransaction();96. 97. Studentstudent=(Student)session.load(Student.class,1);98. System.out.println(=+student.getName();99. session.getTransaction().commit();100. catch(Exceptione)101. e.printStackTrace();102. session.getTransaction().rollback();103. finally104. HibernateUtils.closeSession(session);105. 106. 107. /管理二级缓存108. SessionFactoryfactory=HibernateUtils.getFactory();109. /factory.evict(Student.class);110. factory.evict(Student.class,1);111. 112. try113. session=HibernateUtils.getSession();114. session.beginTransaction();115. 116. /会发出sql,因为二级缓存中的数据被清除了117. Studentstudent=(Student)session.load(Student.class,1);118. System.out.println(=+student.getName();119. session.getTransaction().commit();120. catch(Exceptione)121. e.printStackTrace();122. session.getTransaction().rollback();123. finally124. HibernateUtils.closeSession(session);125. 126. 127. 128. 129. /*130. *一级缓存和二级缓存的交互131. */132. publicvoidtestCache4()133. Sessionsession=null;134. try135. session=HibernateUtils.getSession();136. session.beginTransaction();137. 138. /仅向二级缓存读数据,而不向二级缓存写数据139. session.setCacheMode(CacheMode.GET);140. 141. Studentstudent=(Student)session.load(Student.class,1);142. System.out.println(=+student.getName();143. session.getTransaction().commit();144. catch(Exceptione)145. e.printStackTrace();146. session.getTransaction().rollback();147. finally148. HibernateUtils.closeSession(session);149. 150. 151. try152. session=HibernateUtils.getSession();153. session.beginTransaction();154. 155. Studentstudent=(Student)session.load(Student.class,1);156. System.out.println(=+student.getName();157. session.getTransaction().commit();158. catch(Exceptione)159. e.printStackTrace();160. session.getTransaction().rollback();161. finally162. HibernateUtils.closeSession(session);163. 164. 165. try166. session=HibernateUtils.getSession();167. session.beginTransaction();168. 169. /指向二级缓存放数据,而不从二级缓存读数据170. session.setCacheMode(CacheMode.PUT);171. 172. /会发出sql,因为CacheMode设置为PUT,它不会读取二级缓存中的数据173. Studentstudent=(Student)session.load(Student.class,1);174. System.out.println(stud
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 IEC 60092-302-2:2025 RLV EN Electrical installations in ships - Part 302-2: Low voltage switchgear and controlgear assemblies - Marine power
- 甘蔗知识培训课件
- 瓷砖基础专业知识培训总结
- 基于创新技术的医疗呼叫系统病床分机深度设计与实践
- 工程项目招标签约居间合同8篇
- 父亲朱自清课件
- 诗歌江南课件
- 少儿才艺培训项目计划
- 无线通信行业市场分析
- 2025平谷大桃买卖合同
- 项目经理年中会议汇报
- 小学生美容知识培训内容课件
- T/CCAS 022-2022水泥工业大气污染物超低排放标准
- 全国一等奖初中语文优质课《背影》精品课件
- 诗词地理学古诗词与地理课件
- 急性脑梗死取栓护理课件
- 普通高等医学教育非直属附属医院认定标准测评表(普通高等医学院校临床教学基地建设与医学教育临床基地建设)
- 安全生产“五个一”、“十个一次”活动内容
- 美团外卖服务合同(完整版)
- 客户回访方案
- 贵州省公路路产损害赔(补)偿收费项目及标准
评论
0/150
提交评论