1. 程式人生 > >Hibernate4 No Session found for current thread原因

Hibernate4 No Session found for current thread原因

Hibernate4 與 spring3 整合之後, 如果在取得session 的地方使用了getCurrentSession, 可能會報一個錯:“No Session found for current thread”, 這個錯誤的原因,網上有很多解決辦法, 但具體原因的分析,卻沒有多少, 這裡轉載一個原理分析:

SessionFactory的getCurrentSession並不能保證在沒有當前Session的情況下會自動建立一個新的,這取決於CurrentSessionContext的實現,SessionFactory將呼叫CurrentSessionContext的currentSession()方法來獲得Session。在Spring中,如果我們在沒有配置TransactionManager並且沒有事先呼叫SessionFactory.openSession()的情況直接呼叫getCurrentSession(),那麼程式將丟擲“No Session found for current thread”異常。如果配置了TranactionManager並且通過@Transactional或者宣告的方式配置的事務邊界,那麼Spring會在開始事務之前通過AOP的方式為當前執行緒建立Session,此時呼叫getCurrentSession()將得到正確結果。

然而,產生以上異常的原因在於Spring提供了自己的CurrentSessionContext實現,如果我們不打算使用Spring,而是自己直接從hibernate.cfg.xml建立SessionFactory,並且為在hibernate.cfg.xml
中設定current_session_context_class為thread,也即使用了ThreadLocalSessionContext,那麼我們在呼叫getCurrentSession()時,如果當前執行緒沒有Session存在,則會建立一個繫結到當前執行緒。

Hibernate在預設情況下會使用JTASessionContext,Spring提供了自己SpringSessionContext,因此我們不用配置current_session_context_class,當Hibernate與Spring整合時,將使用該SessionContext,故此時呼叫getCurrentSession()的效果完全依賴於SpringSessionContext的實現。

在沒有Spring的情況下使用Hibernate,如果沒有在hibernate.cfg.xml中配置current_session_context_class,有沒有JTA的話,那麼程式將丟擲"No CurrentSessionContext configured!"異常。此時的解決辦法是在hibernate.cfg.xml中將current_session_context_class配置成thread。

在Spring中使用Hibernate,如果我們配置了TransactionManager,那麼我們就不應該呼叫SessionFactory的openSession()來獲得Sessioin,因為這樣獲得的Session並沒有被事務管理。

至於解決的辦法,可以採用如下方式:
1.  在spring 配置檔案中加入
<tx:annotation-driven transaction-manager="transactionManager"/>



並且在處理業務邏輯的類上採用註解

@Service
public class CustomerServiceImpl implements CustomerService {  
    @Transactional
    public void saveCustomer(Customer customer) {
        customerDaoImpl.saveCustomer(customer);
    }
    ...
}



另外在 hibernate 的配置檔案中,也可以增加這樣的配置來避免這個錯誤:

<property name="current_session_context_class">thread</property>

相關推薦

Hibernate4 No Session found for current thread原因

Hibernate4 與 spring3 整合之後, 如果在取得session 的地方使用了getCurrentSession, 可能會報一個錯:“No Session found for current thread”, 這個錯誤的原因,網上有很多解決辦法, 但具體原因的

SpringMVC3+Hibernate4問題:org.hibernate.HibernateException: No Session found for current thread

問:1:org.hibernate.HibernateException: No Session found for current thread 解決方法: 在web.xml中新增openSessionInViewFilter <filter>

Spring+Hibernate4 Junit 報錯No Session found for current thread

論壇上有另外一篇更全面的帖子,jinnianshilongnian寫的:http://www.iteye.com/topic/1120924  本文的環境是:  spring-framework-3.1.0  hibernate-4.1.6  junit-

SSH dao層異常 org.hibernate.HibernateException: No Session found for current thread

llb exec pan val span ldr cep sch nds 解決方法: 在 接口方法中添加 事務註解 即可。 public interface IBase<PK extends Serializable, T> { @

Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread

背景 使用spring框架整合Hibernate的時候,通過getCurrentSession()獲得與執行緒繫結的session時,可能會遇到no session found for current thread的錯誤; 原因:呼叫getCurrentSession()之前,沒有呼

org.hibernate.HibernateException: No Session found for current thread

<!-- hibernate --> <filter> <filter-name>OpenSessionInViewFilter</filter-nam

getCurrentSession()時報錯,no session found for current thread,解決方案

當我們使用spring框架,通過getCurrentSession()獲得與執行緒繫結的session時,可能會遇到no session found for current thread的錯誤,要解決該錯誤,我們必須先知道該錯誤產生的原因是什麼。 無論我們是在用Hibern

Could not obtain transaction-synchronized Session for current thread原因及解決方案

          在開發中,碰到到了Could not obtain transaction-synchronized Session for current thread異常,因此特意記錄下。 一、問題的產生    

Hibernate4中使用getCurrentSession報Could not obtain transaction-synchronized Session for current thread

Dao層直接注入的sessionFactory,然後用getCurrentSession方法獲取session,然後問題來了,直接報錯:

Maven出現錯誤No plugin found for prefix 'jetty' in the current

jetty jpress Maven出現錯誤No plugin found for prefix ‘jetty‘ in the current project and in the plugin groups的問題解決只需在maven的setting.xml文件上加入如下節點:<pluginGr

Could not obtain transaction-synchronized Session for current thread

ram form string spa 實現 sql 新的 line nal 今天運行程序時 報了 如下的錯誤。 Could not obtain transaction-synchronized Session for current thread 在Spring中使

no plugin found for prefix 'tomcat 7' in the current project

tps prefix detail ref pre mave found details 解決 使用maven build編譯出錯 “no plugin found for prefix ‘tomcat 7‘ in the current project....

No plugin found for prefix 'jetty' in the current project and in the plugin groups

從意思來看,在本地倉庫有jetty的jar包,專案卻找不到,但是我明明在web的pom檔案中添加了jar <plugin> <groupId>org.mortbay.jetty</groupId>

maven:No plugin found for prefix 'jetty' in the current project and in the plugin groups

翻譯如下: 在當前的專案裡或外掛組裡沒有以“jetty”開頭名字的外掛。 怎麼辦?3種方案哪種都行。推薦第二種 1.在外掛組裡新增jetty的外掛 在maven的settings.xml裡新增以

No plugin found for prefix 'install' in the current project解決方案

 本來以下配置,報上面的錯 <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The reposi

多執行緒出現HibernateException: Could not obtain transaction-synchronized Session for current thread

使用hibernate4的SessionFactory獲取Session時有兩種方式 1、sessionFactory.getCurrentSession() 2、sessionFactory.openSession(),開啟新session,需要關閉 當使用1

spring管理事務失效的原因No mapping found for HTTP request with URI []DispatcherServlet with name 'sprinmvc錯誤

練習了一個spring的事務配置問題,在搭建spring和springmvc時,考驗了一下事務,結果發現事務竟然失效了。這裡做一下總結。 一般我們在Spring的配置檔案application.xml中對Service層程式碼配置事務管理,可以對Service的方法進行AOP增強或事務處理如事務

安裝maven所見錯誤No plugin found for prefix 'help' in the current project and in the plugin

安裝maven所見錯誤No plugin found for prefix ‘help’ in the current project and in the plugin 最近兩天在用idea使用maven,可是maven的環境變數配置正確,maven安裝包也

No plugin found for prefix 'jetty' in the current project and in the plugin groups

在maven進行jetty的除錯中出現錯誤: [ERROR] No plugin found for prefix 'jetty' in the current project and in the plu gin groups [org.apache.maven.pl

spring hibernate整合: Could not obtain transaction-synchronized Session for current thread

當hibernate 將事務交由 spring管理時,  應將 @Transactional 加入對接的介面層,即其它物件呼叫的方法上,若將@Transactional加在內部方法上,將會出現 Could not obtain transacti