Hibernate的Session详解

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对象。
posted @ 2017-11-19 12:20  爱喝凉白开  阅读(362)  评论(0编辑  收藏  举报