1>性能区别:
OpenSession效率非常低,每次请求可能会执行多个功能,每个功能都是使用OpenSession,这样会反复的连接数据库,效率低
getCurrentSession首先从当前线程中查询是否有可用的session,如果有直接拿来使用,如果没有,新打开一个,并把他放入当前线程中
2>事务对其影响的差别:
getCurrentSession如果有事务,并且事务提交,则session关闭.
OpenSession事务是否关闭对其没有影响
3>怎样利用getCurrentSession解决OpenSession的问题:
可以将第一次生产的session(getCurrentSession)放入到当前线程中,以方便后期使用.
直接在 hibernate.cfg.xml配置
<!-- 配置getCurrentSession 可以获取当前线程-->
<property name="current_session_context_class">thread</property>
<property name="current_session_context_class">thread</property>