SessionFactory的openSession与getCurrentSession区别

SessionFactory

1 用来产生和管理sesssion

2 通常情况下,每个应用只需要一个SessionFactory,除非要访问多个数据库的情况

3 openSession()与openSession()

(1) openSession()总是创建新的session,需要手动close().

(2)getCurrentSession()事务提交则自动关闭.从上下文环境中获得session,如果当时环境中不存就创建新的.如果环境中存在就使用环境中的,而且每次得到的都是同一个session(在session提交之前,提交之后就是新的了).用途:界定事务边界.

所谓的上下文参见配置文件

<property name="current_session_context_classs">thread</property>

取值范围 jta | thread | managed | custom.Class 

原文地址:https://www.cnblogs.com/seven7seven/p/3868459.html