hibernate3学习笔记(六) Session管理 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-20
return session;30. }31.
32. /*33. * closing the thread-safe session34. */
35. public static void closeSession(){36.
37. Session session = (Session) tLocalsess.get();38. tLocalsess.set(null);39. try{40. if (session != null && session.isOpen()){41. session.close();42. }43.
44. }catch (HibernateException e){45. throw new InfrastructureException(e);46. }47. }48.
49. /*50. * begin the transaction51. */
52. public static void beginTransaction(){53. Transaction tx = (Transaction) tLocaltx.get();54. try{55. if (tx == null){56. tx = currentSession().beginTransaction();57. tLocaltx.set(tx);58. }59. }catch (HibernateException e){60. throw new InfrastructureException(e);61. }62. }63.
64. /*65. * close the transaction66. */
67. public static void commitTransaction(){68. Transaction tx = (Transaction) tLocaltx.get();69. try{70. if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())71. tx.commit();72. tLocaltx.set(null);73. }catch (HibernateException e){74. throw new InfrastructureException(e);75. }76. }77.
78. /*79. * for rollbacking80. */
81. public static void rollbackTransaction(){82. Transaction tx = (Transaction) tLocaltx.get();83. try{84. tLocaltx.set(null);85. if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){86. tx.rollback();87. }88. }catch (HibernateException e){89. throw new InfrastructureException(e);90. }91. }92.
93. private static Session openSession() throws HibernateException{94. return getSessionFactory().openSession();95. }96.
97. private static SessionFactory getSessionFactory() throws HibernateException{98. return SingletonSessionFactory.getInstance();99. }100.}
hibernate3学习笔记(六) Session管理(3)时间:2011-02-02filter中则: 1.public class HibernateSessionCloser implements Filter{2.3. protected FilterConfig filterConfig = null;4. 5. public void init(FilterConfig filterConfig)throws ServletException{6. this.filterConfig = filterConfig;7. }8. 9. public void destroy(){10. this.filterConfig = null;11. } 12. 13. public void doFilter(ServletRequest request, ServletResponse response,14. FilterChain chain)15. throws IOException, ServletException {16. try{17. chain.doFilter(request, response);18. }19. finally{20. try{21. HibernateSessionUtil.commitTransaction();22. }catch (InfrastructureException e){23. HibernateSessionUtil.rollbackTransaction();24. }finally{25. HibernateSessi |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于hibernate3学习笔记(六) Session管理 - 编程入门网的所有评论