No Hibernate Session bound to thread

comeontony發表於2011-11-02

用springside3生成的專案

在App啟動時,系統初始化載入的時候發生下面的錯誤

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
	at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:700)
 

 

把其中的getCurrentSession改成openSession就可以使用,當沒有事物啟動的時候getCurrentSession是無法建立Session的。

 

參考:http://www.iteye.com/topic/87035

opensession是從sessionfactory得到一個新的session,所以可以使用,而getCurrentSession是從當前執行緒 中得到事務開始時建立transaction的那個session,而你的事務沒有能正確的啟動,所以並沒有一個session繫結到當前執行緒,所以你也 得不到。

 

我的最終的解決辦法是通過註解給我的service加上事務即可:@Transactional

相關文章