spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

这个错误,网上说的原因一大推,反正我这个出的问题 是因为 虽然我在 spring 里把事务都配置好了,结果运行就出现这个错误,看看配置都没什么问题,都是网上 案例 照 写编码的,还是错的,结果发现是因为 我 在拿 spring 中 的 service 或 dao bean 时 拿的方式错了。

  

FooService fooService = ctx.getBean("xxxxDao",FooService.class); //不能直接中bean中拿目标代理对象,而是从事务bean中去拿目标代理对象,这样拿到的代理才有spring 事务管理 //FooService fooService = (FooService) ctx.getBean("fooService");

---》 如上边  spring 中是对 FooService 接口 的 子类  xxxFooService 进行 事务管理,原来习惯直接 FooService fooService = (FooService) ctx.getBean("fooService"); -- fooService 为spring中的bean

其实错的,拿到的虽然也是 子类 xxxFooService 的bean ,但是这个 bean是没有 spring 事务管理的,那怎么才能拿到有spring 管理的bean呢,即

FooService fooService = ctx.getBean("xxxxDao",FooService.class);  --  这个  xxxxDao 为 TransactionProxyFactoryBean 的bean 或 是其 子类 bean(当其为abstract时必须拿其子类bean)上边第一个参数 为 事务bean ,第二个参数 为我们想要 的 代理目标对象(必须用接口类,不可用具体实现类),这样拿到的 service 才有 spring 事务管理,改正后上边的报错就不会出现了。
				
时间: 2024-07-29 07:52:54

spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常的相关文章

报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="txManager"/> 2.没有在方法上挂注解事务标签

Spring管理事务配置的五种方式

spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类,一个接口类一个实现类: package com.dao; public interface UserDao { public void getUser(); } package com.dao.impl; import org.springframework.orm.hibernate3.supp

Spring boot使用Redis时,报错,有redisTemplate和stringRedisTemplate两个bean?

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-05-13 20:11:30.651 ERROR 1869 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION

spring 实现事务配置的方式

spring 中常用的两种事务配置方式以及事务的传播性.隔离级别 一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 <!-- 定义事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSource

Spring MVC 事务配置

Spring MVC事务配置 要了解事务配置的所有方法,请看一下<Spring事务配置的5种方法> 本文介绍两种配置方法: 一.      XML,使用tx标签配置拦截器实现事务 一.      Annotation方式 以下所使用环境为Spring4.0.3.Hibernate4.3.5 一.      XML,使用tx标签配置拦截器实现事务 Entity类User.java,持久化类,对应数据库表user package com.lei.demo.entity; import javax.

[转]Spring MVC 事务配置

Spring MVC事务配置 要了解事务配置的所有方法,请看一下<Spring事务配置的5种方法> 本文介绍两种配置方法:  <tx:advice/>就是告诉事务管理器:怎么做.如何去执行.通过什么方法(形式)去执行. 一.      XML,使用tx标签配置拦截器实现事务 一.      Annotation方式 以下所使用环境为Spring4.0.3.Hibernate4.3.5 一.      XML,使用tx标签配置拦截器实现事务 Entity类User.java,持久化类

菜鸟调错(四)——Spring与DWR集成,配置文件报错

背景简介: 该项目是市信用办的一个系统,之前好像是一个石家庄的公司负责的.我属于是半路接手.拿到源码后,根据他们给的简(shao)单(de)明(ke)了(lian)的说明把项目搭起来.结果可想而知,项目文件一片红.于是就开始解决这些错误,好在很多错误都是类似的.经过一番捣鼓,大部分的错误都解决了,有一个Spring跟DWR集成配置的错误,错误信息如下: Multiple annotations found at this line: - schema_reference.4: Failed to

PostgreSQL添加新服务器连接时,报错“Server doesn&#39;t listen ”,已解决。

PostgreSQL添加新的服务器连接时,报错: 解决方法: 第一步:修改配置文件中连接的服务器列表,添加服务器IP地址(图pg002.png) 配置文件地址:数据库右击属性,打开数据库的安装路径在data文件中找到配置文件pg_hba.conf. 如:D:\Program Files\PostgreSQL\9.4\data\pg_hba.conf 注:只要修改IP路径就可以了,‘/’后面的数字都为32. 第二步:修改配置文件后,启动服务器的服务: 启动服务完成,连接服务器,新服务器就可以正常的

【IDEA】创建maven项目时,报错[FATAL_ERROR] Cannot start Maven: Cannot find JRE &#39;1.7&#39;

在使用IDEA使用maven创建springMVC项目时,出现下面的错误,导致无法创建生成正常的springMVC项目结构,而只有一个pom文件: [FATAL_ERROR] Cannot start Maven: Cannot find JRE '1.7' 原因是:我在默认的setting配置里,File-->Other Setting-->Default Setting对应的maven配置里,设置了runner的VM参数:-DarchetypeCatalog=internal,这里需要用到