1. 程式人生 > >getCurrentSession()時報錯,no session found for current thread,解決方案

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

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

無論我們是在用Hibernate4 與 spring3整合還是其他的什麼情況,該錯誤產生的根本原因就是當我們在呼叫getCurrentSession()之前,沒有呼叫SessionFactory.openSession()開啟session。

所以解決方案分兩種情況:

1.如果沒有配置TranactionManager事務,那麼呼叫getCurrentSession()之前,先呼叫SessionFactory.openSession();

2.如果配置了TranactionManager事務,無論是@Transactional註解或者宣告的方式配置的事務邊界,那麼Spring都會在開始事務之前通過AOP的方式為當前執行緒建立Session,此時呼叫getCurrentSession()是沒有問題的。但是如果依然出現了該問題,那麼十之八九是事務沒有起到作用。

①如果是@Transactional事務那麼spring的配置檔案中需要加上

<!-- 啟動事務註解 -->	 
<tx:annotation-driven transaction-manager="transactionManager"/>

② 如果已經加上該註釋依舊沒起作用,那就可能是掃描包的地方有點問題,如果是spring+springMVC框架,因為springMVC容器是spring的子容器,也就是說spring容器的建立是優先於springMVC,那麼如果springMVC對包的掃描沒有過濾掉對service的掃描,就會存在springMVC建立的不含有事務的service替換掉主容器spring建立的含有事務的service,因此,我們的事務就相當於沒有起到作用,解決方案就是springMVC子容器掃描包時過濾掉對service的掃描,程式碼如下:

在spring-mvc.xml檔案中加上:
<context:component-scan base-package="com.lb.yqf" >
<span style="white-space:pre">	</span><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

文筆拙劣,讓各位讀者見笑了,希望本文能夠幫助大家解決問題

最後如果還有疑問可以參閱:

http://www.yihaomen.com/article/java/466.htm

http://blog.csdn.net/z69183787/article/details/37819831

http://bbs.csdn.net/topics/390767253

相關推薦

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

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

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

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>

解決返回JSON時報No converter found for return value of type:

exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentExc

IAR破解時no features found for selected product

        系統是Win10,安裝用於51微控制器開發的版本,破解檔案網上都能下到,這裡給個連結:IAR破解版下載。         直接說解決問題的原因及辦法。  【問題原因】:破解程式的pro

spring報"No property ... found for type..."

遇到很多次這個問題了,可能正是因為沒有寫一篇部落格,才導致我一直踩坑. 這個問題的原因,我遇到的只有一個,就是Spring Data JPA的Entity實體沒有對應好,具體怎麼說呢 這樣,我遇到的問題是這樣,我的資料庫中的欄位是publish_time,然後為了偷懶,在JavaBean中,

SSM報No converter found for return value of type: class java.util.ArrayList at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageC

我使用的是SSM框架,是在編寫測試RESTFUL介面的時候出現, @RequestMapping(value = "/selectAll", method = RequestMethod.GET) @ResponseBody public ResponseEntity<List&

Mybatis啟動報No typehandler found for property xxxx 的另一種可能(由CLOB型別導致的問題)

背景:搭了一個demo專案啟動的時候Mybatis報錯錯誤資訊:Caused by: java.lang.IllegalStateException: No typehandler found for

SSM報No mapping found for HTTP request with URI [/smbms/js/echarts.js] in DispatcherServlet with na

在jsp引用ECharts的js包: 啟動Tomcat,在位址列輸入URL:http://localhost:8080/smbms/user/linechart.html 結果頁面空白,報錯日誌部分資訊: [DEBUG] 2017-11-11 09:32:33,160 o

在訪問RESTful接口時出現:Could not write content: No serializer found for class 的問題解決小技巧收集

onu restfu zab blank rest org lips 收集 規範 註意:市面上對於寫RESTful是用RestFul,其實是不規範的,標準寫法RESTful。 在Java上,處理這個應該是沒有繼承Serializable類,然後通過Eclipse生成ser

No configuration found for the specified action解決辦法

使用Struts2,配置一切正常,使用常用tag也正常,但是在使用<s:form>標記時,發現控制檯總是輸出警告資訊,  警告資訊內容如下: 警告: No configuration found for the specified action: 'ShowMes

mac下編譯安裝php7時報ld: symbol(s) not found for architecture x86_64的解決方法

在Mac上編譯PHP7時,如果使用OpenSSL擴充套件,會有一個錯誤 Undefined symbols for architecture x86_64:   "_PKCS5_PBKDF2_HMAC", referenced from:       _z

no setter found for property 問題的解決

例如:IUsersDAO.java 是介面,而UsersDAO.java是實現IUsersDAO.java 裡的介面,且UserDAO.java必須要繼承自HibernateDaoSupport。若出現

no session 問題出現的原因及解決方案

lte base 請求 屬性 TP IE session關閉 範圍 關聯對象 session是hibernate框架與數據庫交互的對象,與HttpSession是完全不同的東西。session通常與線程綁定,使用完之後就會關閉。 no session問題的出現與延遲加載有關

虛擬機器電腦之間的遷移以及靜態IP的配置以及虛擬機器遷移後重啟網絡卡報:Error:No suitable device found: no device found for connection "S

1.虛擬機器遷移後網絡卡的配置:‘ 自己電腦容量不夠了,準備將資料遷移到公司電腦,但是虛擬機器中做了好多配置,重新安裝會很麻煩,感覺虛擬機器是不是可以直接匯出匯入,保留配置。百度後找到了解決方法。 VMware虛擬機器遷移過程: ’右鍵選擇vmware中的虛擬機器,選擇開啟虛擬機器目錄,

關於dubbo創建服務和引用服務時會報:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 問題的解決

一個 sch 系統 contex ips 分布 配置文件 prot 商城項目   在跟著做淘淘商城項目時,用到了dubbo,作為一個SOA架構的項目,分為表現層與服務層,自然地,為了各個層之間解耦合(或者最大限度地松耦合),我們使用了dubbo這樣一個alibaba開源的分