session简介
.不建议直接通过JDBC的connection操作数据库,而是通过使用session操作数据库。
.session可以理解为操作数据库的对象
.session与connection是多对一的关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用
.把对象保存在关系数据库中需要调用session的各种方法,如:save(),update(),delete(),createQuery()等。
如何获取session对象
1. openSession
2. getCurrentSession
.如果使用getCurrentSession需要在hibernate.cfg.xml文件中进行配置:
.如果是本地事务(jdbc事务)
<propertyname="hibernate.current_session_contect_class">thread</property>
.如果是全局事务(jta事务)
<propertyname="hibernate.current_session_context_class">jta</property>
openSession和getCurrentSession的区别
1. getCurrentSession在事务提交或者回滚之后会自动关闭,而openSession需要手动关闭,如果使用openSession而没有手动关闭,多次之后会导致连接池溢出
2. openSession每次创建新的session对象,getCurrentSession使用现有的session对象
转载请注明原文地址: https://ju.6miu.com/read-7197.html