hibernate中session的产生方式(openSession、getCurrentSession)

1.openSession是直接创建一个session。getCurrentSession是获取当前线程池中的session。

2.session是存放在ThreadLocal中。不会有安全性问题,由于每一个线程仅仅能訪问到当前线程相应的session。
3.getCurrentSession的效率更高。
4.使用getCurrentSession须要在hibernate.cfg.xml中加入属性
    <property name="current_session_context_class">thread</property>
5.openSession在commit后还须要close。getCurrentSession在commit后自己主动close。再close会报错。
6.getCurrentSession时全部的增删改查都须要起事务。

原文地址:https://www.cnblogs.com/wgwyanfs/p/6937875.html