如何獲得session對象?
1)openSession
2)getCurrentSession
如果使用getCurrentSession需要在hibernate.cfg.xml文件中進行配置
如果是本地事務(jdbc事務):
<property name="hibernate.current_session_context_class">thread</property>
如果是全局事務(jta事務):
<property name="hibernate.current_session_context_class">jta</property>
全局事務:資源管理器管理和協(xié)調(diào)的事務,可以跨越多個數(shù)據(jù)庫和進程。資源管理器一般使用 XA 二階段提交協(xié)議與“企業(yè)信息系統(tǒng)”(EIS) 或數(shù)據(jù)庫進行交互。
本地事務:在單個 EIS 或數(shù)據(jù)庫的本地并且限制在單個進程內(nèi)的事務。本地事務不涉及多個數(shù)據(jù)來源。
(1)getCurrentSession在事務提交或者回滾之后會自動關(guān)閉,而openSesssion需要你手動關(guān)閉。
如果使用openSession而沒有手動關(guān)閉,多次之后會導致連接池溢出!
(2)openSession每次創(chuàng)建新的session對象,
getCurrentSession使用現(xiàn)有的session對象(現(xiàn)有session沒commit之前)