thread 屬性" type="hidden"/>

thread 屬性


<property name="current_session_context_class">thread</property>
這個屬性的作用:這樣配置是本地jdbc事務配置,你通過getCurrentSession創建的session會綁定到當前線程

 

 

 

平時在單獨使用hibernate的時候,習慣於配置屬性

<property name="current_session_context_class">thread</property>
根據文檔,這個是hibernate3.1以后的一個新擴展,目的在於可以讓我們在某一個上下文環境(比如說當前線程)中可以通過 SessionFactory.getCurrentSession()得到同一個session會話.
 
后來當我們把spring,hibernate整合的時候,在spring的主配置文件當中,我們也習慣於帶入這樣的配置
<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
,接下來在spring配置文件中,會使用spring2.x的聲明式的方式來配置事務
<tx:advice id="txAdvice" transaction-manager="transactionManager">,<aop:pointcut,<aop:advisor等等配置指定哪些方法上由spring來管理hiberante的事務,這個時候我們試着運行一個類似於這樣的方法
public void find() {
Session se = sf.getCurrentSession();
//此處不需要se.beginTransaction(),事務已經交由spring管理
Dept d = (Dept) se.get(Dept.class, new Long(12));
}
會得到一個異常: get is not valid without active transaction.
這個錯誤一般情況是因為由getCurrentSession得到的session中沒有獲得的transaction,我們一般要手動的調用se.beginTransaction(),來打開一個活動的事務.但是問題是,我們在spring的配置文件中不是已經通過aop,指定了此處由spring來管理事務嗎,怎么還要手動處理事務?
 
答案: 
<prop key="hibernate.current_session_context_class">thread</prop>改為
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>(默認配置)
 
參考:
1. hibernate文檔:
 
2.在hibernate中,thread,jta,manager的配置其實都是對應了3個hibernate的實現類
 
 
 
 
3.在sessionFactory配置文件中
應將hibernate.current_session_context_class設為org.springframework.orm.hibernate3.SpringSessionContext(默認為此值)應當把session要綁定的上下文設置為由spring環境管理(因為我們的事務aop也是在spring范圍中),這個時候spring中的事務配置才會起作用(當然,以前是thread上下文環境的session,而事務托管在spring上下文中,當然spring無法管理到thread上下文的session的事務咯)綜上所述,其實仔細看ThreadLocalSessionContext,JTASessionContext,ManagedSessionContext,SpringSessionContext我們都會發現,他們都實現了一個接口org.hibernate.context.CurrentSessionContext關於這個接口,請參閱 org.hibernate.context.CurrentSessionContext 接口的 Javadoc,那里有關於它的契約的詳細討論。它定義了單一的方法,currentSession(),特定的實現用它來負責跟蹤當前的上下文相關的會話 最后,我們知道,其實線程綁定也好,上下文綁定也好,最后都是,使用實現了CurrentSessionContext接口的一個類,來跟蹤session,然后我們通過這個類的對象來獲得被它跟蹤的session,以達到在我們定義的上下文環境中調用getCurrentSession方法獲得的總是同一個session


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM