getCurrentSession()與openSession()的區別?
* 采用getCurrentSession()創建的session會綁定到當前線程中,而采用openSession()
創建的session則不會
* 采用getCurrentSession()創建的session在commit或rollback時會自動關閉,而采用openSession()
創建的session必須手動關閉
使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事務(jdbc事務)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事務(jta事務)
<property name="hibernate.current_session_context_class">jta</property>