首页 > Web开发 > 详细

Hibernate的Session详解

时间:2017-11-19 13:07:29      阅读:200      评论:0      收藏:0      [点我收藏+]
Hibernate操纵数据库都是通过Session对象来实现,Session对象表示数据库的连接
 
获得Session的方法有两种:
1)openSession
2)getCurrentSession
 
使用第二种需要在hibernate.cfg.xml文件中进行配置:
如果是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
如果是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
 
openSession和getCurrentSession的区别:
1.getCurrentSession在事务提交或者回滚之后会自动关闭,而openSession需要手动关闭。如果使用openSession而没有手动关闭,多次之后回导致连接池的溢出。
 
2.openSession每次创建新的session对象,getCurrentSession使用现有的session对象。

Hibernate的Session详解

原文:http://www.cnblogs.com/huangd/p/7859456.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!