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

spring、springmvc和hibernate整合

在sessionFactory.getCurrentSession()時,出現以下異常
No Session found for current thread

但使用sessionFactory.openSession()是沒有任何問題的
嚴重: Servlet.service() for servlet [springDispatcherServlet] in context with path [/Demo] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] with root cause
org.hibernate.HibernateException: No Session found for current threadat org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)at com.wzy.dao.BookShopDaoImpl.getAll(BookShopDaoImpl.java:16)at com.wzy.services.impl.BookSerImpl.getALL(BookSerImpl.java:19)at com.wzy.controller.Test.hello(Test.java:36)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)
1. getCurrentSession和openSession之間有什么不同呢

getCurrentSession的話會自動關閉,而openSession需要你手動關閉。
如果你正在查詢,使用的openSession而沒有手動關閉,多次之后會導致連接池溢出。
getCurrentSession是與當前線程綁定的,當前線程結束時,session也會自動關閉
getCurrentSession是比較安全的,建議使用
2. 獲取getCurrentSession時,為什么會出現以上異常
是因為沒有配置事務
spring hibernate事務的流程
在方法開始之前,獲取session,把session和當前線程綁定,這樣就可以在dao中使用sessionFactory.getCurrentSession()來獲取session了,然后開啟事務。

若方法正常結束,即沒有異常,則提交事務,解除和當前線程綁定的session,關閉session。

若方法出現異常,則回滾事務,解除綁定,關閉session。



3. 怎么配置事務
如下是spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><context:component-scan base-package="com.wzy"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><context:property-placeholder location="classpath:db.properties"/><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="initialPoolSize" value="${jdbc.initPoolSize}"></property><property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="mappingLocations" value="classpath:com/wzy/entities/*.hbm.xml"></property><!--<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>--><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!-- 配置 Spring 的聲明式事務 1. 配置 hibernate 的事務管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!--  2. 配置事務屬性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice><!--  3. 配置事務切入點, 再把事務屬性和事務切入點關聯起來<aop:config><aop:pointcut expression="execution(* com.wzy.services.*.*(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>--> </beans>

?4. 為什么我spring的配置文件已經配置好了,還是會出那個異常呢

如果是spring、strut2、hibernate整合的話,事務在spring的配置文件中配置就行

但如果是spring、springmvc、hibernate整合的話,事務切入點得配置在springmvc的配置文件中

因為spring和springmvc是兩個容器

只需要把spring配置文件中的<!-- 3. 配置事務切入點, 再把事務屬性和事務切入點關聯起來-->

放到springmvc的配置文件中就可以了,spring中的那塊就可以去掉了

如下是springMVC的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><context:component-scan base-package="com.wzy"></context:component-scan><!--  3. 配置事務切入點, 再把事務屬性和事務切入點關聯起來--><aop:config><aop:pointcut expression="execution(* com.wzy.services.*.*(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>
</beans>

?

總的來說,當spring、springmvc和hibernate整合時
在sessionFactory.getCurrentSession()時,出現No Session found for current thread
需要配置事務,并且在springmvc配置文件中配置事務的切入點

?







?

轉載于:https://www.cnblogs.com/wwzyy/p/5560935.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/287454.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/287454.shtml
英文地址,請注明出處:http://en.pswp.cn/news/287454.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java mysbatis select_MyBatis SELECT基本查詢實現方法詳解

1、返回一個LISTselect * from tbl_employee where last_name like #{lastName}2、將查詢記錄封裝為一個Mapselect * from tbl_employee where id#{id}返回一條記錄的map&#xff1b;key就是列名&#xff0c;值就是對應的值。3、多條記錄封裝為一個mapMapKey("id")pu…

Git之怎么通過命令修改前面幾次提交的記錄

1 問題 我們平時用gitlab,github發現提交代碼上庫記錄寫錯了&#xff0c;需要修改回來。 2 解決辦法

Git客戶端TortoiseGit(Windows系統)的使用方法

本文環境&#xff1a; 操作系統&#xff1a;Windows XP SP3 Git客戶端&#xff1a;TortoiseGit-1.8.8.0-32bit 一、安裝Git客戶端 全部安裝均采用默認&#xff01; 1. 安裝支撐軟件 msysgit: https://code.google.com/p/msysgit/downloads/list?qfullinstallerofficialgit 當前…

.Net 在容器中操作宿主機

1方案描述 在 docker 容器中想操作宿主機&#xff0c;一般會使用 ssh 的方式&#xff0c;然后 .Net 通過執行遠程 ssh 指令來操作宿主機。本文將使用 交互式 .Net 容器版 中提供的鏡像演示 .Net 在容器中如何操作宿主機。2前期準備 1. 宿主機上生成 ssh key生成 ss…

【看動漫學編程】程序員在異世界生個娃 第1篇:太極村

前言 作者文筆比較水&#xff0c;還請見諒。 以下內容還將使用視頻動態漫畫表現&#xff0c;剪輯完將會貼出鏈接。 小說劇情為劇情需要&#xff0c;過渡到知識點&#xff0c;部分篇幅可能沒有技術知識點還望諒解。 由于沒有經費支持&#xff0c;所以畫出來的東西是我自己用代碼…

【ArcGIS風暴】最牛逼空間數據批處理神器來了:用戶自定義工具箱GeoStorm.tbx

【Warming up】在學習和工作的過程中,作者曾寫過很多采用ArcGIS模型構建器(Model Builder)、Python代碼等批處理方法(感興趣的GISers可以去【測繪地理信息Big風暴專】欄去交流學習指導),大大的減輕了操作壓力,提高了工作效率。今天給大家展示一款神器:自定義工具箱GeoS…

2.6. PostgreSQL表之間連接

到目前為止&#xff0c;我們的查詢一次只訪問了一個表。查詢可以一次訪問多個表&#xff0c;或者用某種方式訪問一個表&#xff0c;而同時處理該表的多個行。一個同時訪問同一個或者不同表的多個行的查詢叫連接查詢。舉例來說&#xff0c;比如你想列出所有天氣記錄以及這些記錄…

Android之Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains

1 問題 用takePhoto去照相的時候特么的一打開就報這個錯誤 2020-04-09 21:33:49.124 19016-19016/com.appsinnova.android.keepshare E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.appsinnova.android.keepshare, PID: 19016java.lang.RuntimeException: Unable to …

Linux下c/c++項目代碼覆蓋率的產生方法

最近做了一系列的單元測試相關的工作&#xff0c;除了各種規范及測試框架以外&#xff0c;討論比較多的就是關于代碼覆蓋率的產生&#xff0c;c/c與其他的一些高級語言或者腳本語言相比較而言&#xff0c;例如 Java、.Net和php/python/perl/shell等&#xff0c;由于沒有這些高級…

C# WPF從后臺代碼生成行列可變的表格

z概述WPF常用的表格控件是DataGrid&#xff0c;這個控件在前臺XAML編寫的話&#xff0c;一般列已經固定&#xff0c;然后給每個列去綁定數據&#xff0c;但是如果我的列不固定&#xff0c;隨著運算結果變動呢&#xff1f;這時候DataGrid&#xff0c;就比較難實現這個需求&#…

軟件架構實踐文章鏈接

2019獨角獸企業重金招聘Python工程師標準>>> 架構 InfoQ: 又拍網架構中的分庫設計 SNS網站數據庫技術分析 - 51CTO.COM 數據庫水平切分的實現原理解析 - iBATIS - Java - JavaEye論壇 基于amoeba的mysql分布式數據庫學習&#xff08;一&#xff09; - Java - JavaEy…

【看動漫學編程】程序員在異世界生個娃 第2篇:外掛已準備就緒

前言 作者文筆比較水&#xff0c;還請見諒。 以下內容還將使用視頻動態漫畫表現&#xff0c;剪輯完將會貼出鏈接。 小說劇情為劇情需要&#xff0c;過渡到知識點&#xff0c;部分篇幅可能沒有技術知識點還望諒解。 由于沒有經費支持&#xff0c;所以畫出來的東西是我自己用代碼…

java剪切txt文件_用Java把剪切板的內容實時保存到txt

test類&#xff1a;提示用戶程序已啟動&#xff0c;提示保存位置&#xff0c;清空剪切板。package com.ariya.service;import com.ariya.service.impl.ClipboardServiceImpl;/*** author Ariya* 程序入口*/public class Test {public static void main(String[] args) {Clipboa…

【三維激光掃描】第一章:三維激光掃描入門基礎知識

隨著地理空間信息服務產業的快速發展,地理空間數據的要求越來越高。對地理空間數據的要求正朝著大信息量、高精度、可視化和可挖掘方向發展。地面激光雷達技術是一門新興的測繪技術,已逐漸成為廣大科研和工程技術人員全新的解決問題的手段。地面三維激光掃描技術與全站儀測量…

Android之kotlin里面本地圖片BitmapFactory.decodeFile轉bitmap失敗問題

1 問題 我們手機本地有個圖片文件比如如下 /storage/emulated/0/Android/data/package_name/cache/1586444511539.png 我們需要png轉bitmap&#xff0c;然后設置到ImageView里面顯示 var bitmap BitmapFactory.decodeFile(imagePath);if (bitmap null) returnelse mImagevi…

3、面向對象-繼承-多態

1、繼承子類可以繼承父類的一切&#xff0c;一個子類只能有一個父類&#xff0c;一個父類可以有多個子類//父類class Ren{public $name;public $sex;public $yuyan;function Say() {echo $this->name."正在講話&#xff01;";}}//美國人的子類class America ex…

整理iOS9適配中出現的坑

一、NSAppTransportSecurity iOS9讓所有的HTTP默認使用了HTTPS&#xff0c;原來的HTTP協議傳輸都改成TLS1.2協議進行傳輸。直接造成的情況就是App發請求的時候彈出網絡無法連接。解決辦法就是在項目的info.plist 文件里加上如下節點&#xff1a; NSAppTransportSecurity - NSAl…

由c# dynamic是否裝箱引發的思考

前言前幾天在技術群里看到有同學在討論關于dynamic是否會存在裝箱拆箱的問題,我當時第一想法是"會"。至于為啥會有很多人有這種疑問&#xff0c;主要是因為覺得dynamic可能是因為有點特殊&#xff0c;因為它被稱為動態類型,可能是因為這里的動態對大家造成的誤解,認為…

【看動漫學編程】程序員在異世界生個娃 第3篇:搞不好我就是個王者

前言 作者文筆比較水&#xff0c;還請見諒。 以下內容還將使用視頻動態漫畫表現&#xff0c;剪輯完將會貼出鏈接。 小說劇情為劇情需要&#xff0c;過渡到知識點&#xff0c;部分篇幅可能沒有技術知識點還望諒解。 由于沒有經費支持&#xff0c;所以畫出來的東西是我自己用代碼…

PHP會話控制考察點

為什么要使用會話控制技術 HTTP協議是無狀態的&#xff0c;也就是說HTTP沒有一個內建的機制來維護兩個事務之間的狀態。當一個用戶完成一個請求發起第二個請求的時候&#xff0c;服務器無法知道這次請求是來自于上一次的客戶。而用戶登錄、購物車等&#xff0c;這些是需要服務器…