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