1 getCurrentSession建立的session會和綁定到當前線程,而openSession每次建立新的session。java
2 getCurrentSession建立的線程會在事務回滾或事物提交後自動關閉,而openSession必須手動關閉數據庫
這裏getCurrentSession本地事務(本地事務:jdbc)時 要在配置文件裏進行以下設置編程
* 若是使用的是本地事務(jdbc事務)
<property name="hibernate.current_session_context_class">thread</property>
* 若是使用的是全局事務(jta事務)
<property name="hibernate.current_session_context_class">jta</property> session
getCurrentSession () 在事務結束以前使用當前的session
openSession() 每次從新創建一個新的session多線程
在一個應用程序中,若是DAO 層使用Spring 的hibernate 模板,經過Spring 來控制session 的生命週期,則首選getCurrentSession ()。併發
使用Hibernate的大多數應用程序須要某種形式的「上下文相關的」 session,特定的session在整個特定的上下文範圍內始終有效。然而,對不一樣類型的應用程序而言,要爲何是組成這種「上下文」下一個定義一般 是困難的;不一樣的上下文對「當前」這個概念定義了不一樣的範圍。在3.0版本以前,使用Hibernate的程序要麼採用自行編寫的基於 ThreadLocal的上下文session,要麼採用HibernateUtil這樣的輔助類,要麼採用第三方框架(好比Spring或Pico), 它們提供了基於代理(proxy)或者基於攔截器(interception)的上下文相關session。框架
從3.0.1版本開 始,Hibernate增長了SessionFactory.getCurrentSession()方法。一開始,它假定了採用JTA事務,JTA事務 定義了當前session的範圍和上下文(scope and context)。Hibernate開發團隊堅信,由於有好幾個獨立的JTA TransactionManager實現穩定可用,不管是否被部署到一個J2EE容器中,大多數(倘若不是全部的)應用程序都應該採用JTA事務管理。 基於這一點,採用JTA的上下文相關session能夠知足你一切須要。ui
更好的是,從3.1開 始,SessionFactory.getCurrentSession()的後臺實現是可拔插的。所以,咱們引入了新的擴展接口 (org.hibernate.context.CurrentSessionContext)和新的配置參數 (hibernate.current_session_context_class),以便對什麼是「當前session」的範圍和上下文(scope and context)的定義進行拔插。spa
請參閱 org.hibernate.context.CurrentSessionContext接口的Javadoc,那裏有關於它的契約的詳細討論。它定義 了單一的方法,currentSession(),特定的實現用它來負責跟蹤當前的上下文session。Hibernate內置了此接口的兩種實現。hibernate
org.hibernate.context.JTASessionContext - 當前session根據JTA來跟蹤和界定。這和之前的僅支持JTA的方法是徹底同樣的。詳情請參閱Javadoc。
org.hibernate.context.ThreadLocalSessionContext - 當前session經過當前執行的線程來跟蹤和界定。詳情也請參閱Javadoc。
這 兩種實現都提供了「每數據庫事務對應一個session」的編程模型,也稱做每次請求一個session。Hibernate session的起始和終結由數據庫事務的生存來控制。倘若你採用自行編寫代碼來管理事務(好比,在純粹的J2SE,或者 JTA/UserTransaction/BMT),建議你使用Hibernate Transaction API來把底層事務實現從你的代碼中隱藏掉。若是你在支持CMT的EJB容器中執行,事務邊界是聲明式定義的,你不須要在代碼中進行任何事務或 session管理操做。請參閱第 11 章 事務和併發一節來閱讀更多的內容和示例代碼。
hibernate.current_session_context_class 配置參數定義了應該採用哪一個org.hibernate.context.CurrentSessionContext實現。注意,爲了向下兼容,若是未 配置此參數,可是存在org.hibernate.transaction.TransactionManagerLookup的配 置,Hibernate會採用org.hibernate.context.JTASessionContext。通常而言,此參數的值指明瞭要使用的實 現類的全名,但那兩個內置的實現可使用簡寫,即"jta"和"thread"。
一、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>
利於ThreadLocal模式管理Session
早在Java1.2推出之時,Java平臺中就引入了一個新的支持:java.lang.ThreadLocal,給咱們在編寫多線程程序
時提供了一種新的選擇。ThreadLocal是什麼呢?其實ThreadLocal並不是是一個線程的本地實現版本,它並非一個Thread,
而是thread local variable(線程局部變量)。也許把它命名爲ThreadLocalVar更加合適。線程局部變量(ThreadLocal)
其實的功用很是簡單,就是爲每個使用某變量的線程都提供一個該變量值的副本,是每個線程均可以獨立地改變本身的副本,
而不會和其它線程的副本衝突。從線程的角度看,就好像每個線程都徹底擁有一個該變量。
ThreadLocal是如何作到爲每個線程維護變量的副本的呢?其實實現的思路很簡單,在ThreadLocal類中有一個Map,
用於存儲每個線程的變量的副本。好比下面的示例實現(爲了簡單,沒有考慮集合的泛型):
public class HibernateUtil { public static final ThreadLocal session =new ThreadLocal(); public static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session currentSession() throws HibernateException { Session s = session.get(); if(s == null) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = session.get(); if(s != null) { s.close(); } session.set(null); } }
在這裏比較了下getCurrentSession()是不是用的是同一個session...............
package com.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class HibernateIDTest { private static SessionFactory sessionFactory; @BeforeClass public static void beforeClass() { try{ sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } @AfterClass public static void afterClass() { sessionFactory.close(); } @Test public void testTeacherSave() { System.out.println("------------"); Teacher t = new Teacher(); t.setId(1); t.setName("t1"); t.setTitle("middle"); t.setBirthDate(new Date()); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); Session session2 = sessionFactory.getCurrentSession(); System.out.println("比較"+(session.hashCode()==session2.hashCode())); } }
執行以後不的到結果是 session.hashCode()==session2.hashCode()這兩個只是false的,也就是說,在事務結束以後getCuttentSession 也是建立了新的session。。。。。。
openSession() 與 getCurrentSession() 有何不一樣和關聯呢?
在 SessionFactory 啓動的時候, Hibernate 會根據配置建立相應的 CurrentSessionContext ,在 getCurrentSession() 被調用的時候,實際被執行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 執行時,若是當前 Session 爲空, currentSession 會調用 SessionFactory 的 openSession 。因此 getCurrentSession() 對於 Java EE 來講是更好的獲取 Session 的方法。