此設(shè)置的作用如下:
What does sessionFactory.getCurrentSession() do? First, you can call it
as many times and anywhere you
like, once you get hold of your SessionFactory (easy thanks to
HibernateUtil). The getCurrentSession()
method always returns the "current" unit of work. Remember that we
switched the configuration option for this
mechanism to "thread" in hibernate.cfg.xml? Hence, the scope of the
current unit of work is the current Java
thread that executes our application. However, this is not the full
truth. A Session begins when it is first
needed, when the first call to getCurrentSession() is made. It is then
bound by Hibernate to the current
thread. When the transaction ends, either committed or rolled back,
Hibernate also unbinds the Session from
the thread and closes it for you. If you call getCurrentSession() again,
you get a new Session and can start a
new unit of work. This thread-bound programming model is the most
popular way of using Hibernate.
意思是說(shuō):
sessionFactory.getCurrentSession()可以完成一系列的工作,當(dāng)調(diào)用時(shí),
hibernate將session綁定到當(dāng)前線程,事務(wù)結(jié)束后,hibernate
將session從當(dāng)前線程中釋放,并且關(guān)閉session。當(dāng)再次調(diào)用getCurrentSession
()時(shí),將得到一個(gè)新的session,并重新開(kāi)始這一系列工作。
這樣調(diào)用方法如下:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
不需要close session了。
前提是改值設(shè)置為了thread.
1 getCurrentSession創(chuàng)建的session會(huì)和綁定到當(dāng)前線程,而openSession不會(huì)。
2 getCurrentSession創(chuàng)建的線程會(huì)在事務(wù)回滾或事物提交后自動(dòng)關(guān)閉,而openSession必須手動(dòng)關(guān)閉
這里getCurrentSession本地事務(wù)(本地事務(wù):jdbc)時(shí) 要在配置文件里進(jìn)行如下設(shè)置
* 如果使用的是本地事務(wù)(jdbc事務(wù))
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事務(wù)(jta事務(wù))
<property name="hibernate.current_session_context_class">jta</property>
getCurrentSession () 使用當(dāng)前的session
openSession() 重新建立一個(gè)新的session
在一個(gè)應(yīng)用程序中,如果DAO 層使用Spring 的hibernate 模板,通過(guò)Spring 來(lái)控制session 的生命周期,則首選getCurrentSession ()。
使 用Hibernate的大多數(shù)應(yīng)用程序需要某種形式的“上下文相關(guān)的”session,特定的session在整個(gè)特定的上下文范圍內(nèi)始終有效。然而,對(duì) 不同類型的應(yīng)用程序而言,要為什么是組成這種“上下文”下一個(gè)定義通常是困難的;不同的上下文對(duì)“當(dāng)前”這個(gè)概念定義了不同的范圍。在3.0版本之前,使 用Hibernate的程序要么采用自行編寫的基于ThreadLocal的上下文session,要么采用HibernateUtil這樣的輔助類,要 么采用第三方框架(比如Spring或Pico),它們提供了基于代理(proxy)或者基于攔截器(interception)的上下文相關(guān) session。
從3.0.1版本開(kāi)始,Hibernate增加了 SessionFactory.getCurrentSession()方法。一開(kāi)始,它假定了采用JTA事務(wù),JTA事務(wù)定義了當(dāng)前session的范 圍和上下文(scope and context)。Hibernate開(kāi)發(fā)團(tuán)隊(duì)堅(jiān)信,因?yàn)橛泻脦讉€(gè)獨(dú)立的JTATransactionManager實(shí)現(xiàn)穩(wěn)定可用,不論是否被部署到一個(gè) J2EE容器中,大多數(shù)(假若不是所有的)應(yīng)用程序都應(yīng)該采用JTA事務(wù)管理?;谶@一點(diǎn),采用JTA的上下文相關(guān)session可以滿足你一切需要。
更 好的是,從3.1開(kāi)始,SessionFactory.getCurrentSession()的后臺(tái)實(shí)現(xiàn)是可拔插的。因此,我們引入了新的擴(kuò)展接口 (org.hibernate.context.CurrentSessionContext)和新的配置參數(shù) (hibernate.current_session_context_class),以便對(duì)什么是“當(dāng)前session”的范圍和上下文 (scopeand context)的定義進(jìn)行拔插。
請(qǐng)參閱 org.hibernate.context.CurrentSessionContext接口的Javadoc,那里有關(guān)于它的契約的詳細(xì)討論。它定義 了單一的方法,currentSession(),特定的實(shí)現(xiàn)用它來(lái)負(fù)責(zé)跟蹤當(dāng)前的上下文session。Hibernate內(nèi)置了此接口的兩種實(shí)現(xiàn)。
org.hibernate.context.JTASessionContext - 當(dāng)前session根據(jù)JTA來(lái)跟蹤和界定。這和以前的僅支持JTA的方法是完全一樣的。詳情請(qǐng)參閱Javadoc。
org.hibernate.context.ThreadLocalSessionContext - 當(dāng)前session通過(guò)當(dāng)前執(zhí)行的線程來(lái)跟蹤和界定。詳情也請(qǐng)參閱Javadoc。
這 兩種實(shí)現(xiàn)都提供了“每數(shù)據(jù)庫(kù)事務(wù)對(duì)應(yīng)一個(gè)session”的編程模型,也稱作每次請(qǐng)求一個(gè)session。Hibernatesession的起始和終結(jié) 由數(shù)據(jù)庫(kù)事務(wù)的生存來(lái)控制。假若你采用自行編寫代碼來(lái)管理事務(wù)(比如,在純粹的J2SE,或者JTA/UserTransaction/BMT),建議你 使用Hibernate TransactionAPI來(lái)把底層事務(wù)實(shí)現(xiàn)從你的代碼中隱藏掉。如果你在支持CMT的EJB容器中執(zhí)行,事務(wù)邊界是聲明式定義的,你不需要在代碼中進(jìn) 行任何事務(wù)或session管理操作。請(qǐng)參閱第 11 章 事務(wù)和并發(fā)一節(jié)來(lái)閱讀更多的內(nèi)容和示例代碼。
hibernate.current_session_context_class 配置參數(shù)定義了應(yīng)該采用哪個(gè)org.hibernate.context.CurrentSessionContext實(shí)現(xiàn)。注意,為了向下兼容,如果未 配置此參數(shù),但是存在org.hibernate.transaction.TransactionManagerLookup的配 置,Hibernate會(huì)采用org.hibernate.context.JTASessionContext。一般而言,此參數(shù)的值指明了要使用的實(shí) 現(xiàn)類的全名,但那兩個(gè)內(nèi)置的實(shí)現(xiàn)可以使用簡(jiǎn)寫,即"jta"和"thread"。
1、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)閉2、使用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>
利于ThreadLocal模式管理Session
早在Java1.2推出之時(shí),Java平臺(tái)中就引入了一個(gè)新的支持:java.lang.ThreadLocal,給我們?cè)诰帉懚嗑€程程序
時(shí)提供了一種新的選擇。ThreadLocal是什么呢?其實(shí)ThreadLocal并非是一個(gè)線程的本地實(shí)現(xiàn)版本,它并不是一個(gè)Thread,
而是thread local variable(線程局部變量)。也許把它命名為ThreadLocalVar更加合適。線程局部變量(ThreadLocal)
其實(shí)的功用非常簡(jiǎn)單,就是為每一個(gè)使用某變量的線程都提供一個(gè)該變量值的副本,是每一個(gè)線程都可以獨(dú)立地改變自己的副本,
而不會(huì)和其它線程的副本沖突。從線程的角度看,就好像每一個(gè)線程都完全擁有一個(gè)該變量。
ThreadLocal是如何做到為每一個(gè)線程維護(hù)變量的副本的呢?其實(shí)實(shí)現(xiàn)的思路很簡(jiǎn)單,在ThreadLocal類中有一個(gè)Map,
用于存儲(chǔ)每一個(gè)線程的變量的副本。比如下面的示例實(shí)現(xiàn)(為了簡(jiǎn)單,沒(méi)有考慮集合的泛型):
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);
}
}
openSession() 與 getCurrentSession() 有何不同和關(guān)聯(lián)呢?
在 SessionFactory 啟動(dòng)的時(shí)候, Hibernate 會(huì)根據(jù)配置創(chuàng)建相應(yīng)的 CurrentSessionContext ,在 getCurrentSession() 被調(diào)用的時(shí)候,實(shí)際被執(zhí)行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 執(zhí)行時(shí),如果當(dāng)前 Session 為空, currentSession 會(huì)調(diào)用 SessionFactory 的 openSession 。所以 getCurrentSession() 對(duì)于 Java EE 來(lái)說(shuō)是更好的獲取 Session 的方法。