采用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>