<property name="current_session_context_class">thread</property>
平時在單獨使用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配置文件中
org.hibernate.context.CurrentSessionContext 接口的 Javadoc,那里有關於它的契約的詳細討論。它定義了單一的方法,currentSession() ,特定的實現用它來負責跟蹤當前的上下文相關的會話 最后,我們知道,其實線程綁定也好,上下文綁定也好,最后都是,使用實現了CurrentSessionContext接口的一個類,來跟蹤session,然后我們通過這個類的對象來獲得被它跟蹤的session,以達到在我們定義的上下文環境中調用getCurrentSession方法獲得的總是同一個session |