Spring重复扫描导致事务失败的解决方案及深入分析
问题及日志
使用Spring和mybatis,然后配置事务,出现SqlSession was not registered for synchronization because synchronization is not active,事务没有启用成功。
[org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
[org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1a714e6e] was not registered for synchronization because synchronization is not active
[org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
[org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [jdbc: mysql://localhost:3306/iot, UserName=root@localhost, MySQL Connector Java]* will not be managed by Spring*
[com.haoyifen.mySSMTemplate.dao.UserMapper.selectByPrimaryKey] - ==> Preparing: select ID, USER_ID, USER_NAME, SEX, ADDRESS, PHONE_NUMBER, MAIL_BOX from user where ID = ?
[com.haoyifen.mySSMTemplate.dao.UserMapper.selectByPrimaryKey] - ==> Parameters: 1(Integer)
[com.haoyifen.mySSMTemplate.dao.UserMapper.selectByPrimaryKey] - <== Total: 1
[org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1a714e6e]
IntelliJ IDEA的编辑器显示我的拦截路径是正确的。为UserService类创建事务配置。
问题分析及解决方案
后来深入分析才发现,原来是扫描配置错误。
我把Spring的ApplicationContext和DispatcherServlet(WebApplicationContext)配置使用两个文件分离开的,分别为spring.xml和spring-mvc.xml。在spring.xml中配置了事务。在两个文件中都启用了扫描设置,用于扫描@Service,@Component和@Controller
spring.xml
<!-- 自动扫描 -->
<context:component-scan base-package="com.haoyifen.mySSMTemplate"></context:component-scan>
1
2
3
spring-mvc.xml
<context:component-scan base-package="com.haoyifen.mySSMTemplate"></context:component-scan>
1
2
3
以上的配置使得spring-mvc重复扫描了userService类,而spring如果要使事务生效,就需要cglib为userService生成代理子类,在spring.xml中已经生成了代理类,而在spring-mvc.xml中,又重新扫描了一遍,使得原先cglib生成的代理子类失效,从而事务拦截也失效。所以我们应该设置spring-mvc.xml,使其不扫描@Service和@Component,spring.xml不扫描@Controller。配置如下:
spring.xml
<!-- 自动扫描 ,忽略@Controller注解的类-->
<context:component-scan base-package="com.haoyifen.mySSMTemplate">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
1
2
3
4
5
spring-mvc.xml
<!--自动扫描该包,使spring-mvc只扫描controller包中的类(其中只有@Controller控制器),不会重复扫描到@Service或者@Component-->
<context:component-scan base-package="com.haoyifen.mySSMTemplate.controller">
</context:component-scan>
1
2
3
4
验证
修改后,再查看日志,就发现已经可以正确启用事务了。
[org.springframework.jdbc.datasource.DataSourceTransactionManager] - Acquired Connection [jdbc:mysql://localhost:3306/iot, UserName=root@localhost, MySQL Connector Java] for JDBC transaction
[org.springframework.jdbc.datasource.DataSourceTransactionManager] - Switching JDBC Connection [jdbc:mysql://localhost:3306/iot, UserName=root@localhost, MySQL Connector Java]* to manual commit*
[org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
[org.mybatis.spring.SqlSessionUtils] -* Registering transaction synchronization for SqlSession* [org.apache.ibatis.session.defaults.DefaultSqlSession@1031238e]
[org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [jdbc:mysql://localhost:3306/iot, UserName=root@localhost, MySQL Connector Java]* will be managed by Spring*
….
[org.mybatis.spring.SqlSessionUtils] -* Transaction synchronization closing SqlSession* [org.apache.ibatis.session.defaults.DefaultSqlSession@1031238e]
[org.springframework.jdbc.datasource.DataSourceTransactionManager] - Initiating transaction commit
[org.springframework.jdbc.datasource.DataSourceTransactionManager] - Committing JDBC transaction on Connection [jdbc:mysql://localhost:3306/iot, UserName=root@localhost, MySQL Connector Java]
[org.springframework.jdbc.datasource.DataSourceTransactionManager] - Releasing JDBC Connection [jdbc:mysql://localhost:3306/iot, UserName=root@localhost, MySQL Connector Java] after transaction
[org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
深入日志分析
我们可以查看日志,来分析一下userService的创建过程。在修改xml文件之前:
Application初始化
创建Application时创建了一次userService,并使用cglib代理子类,生成了事务类:
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring.xml]
…..
开始创建userService类
[org.springframework.beans.factory.support.DefaultListableBeanFactory] -* Creating shared instance of singleton bean ‘userService’*
[org.springframework.beans.factory.support.DefaultListableBeanFactory] - Creating instance of bean ‘userService’
……
缓存userService用于其他bean的注入
[org.springframework.beans.factory.support.DefaultListableBeanFactory] - Eagerly caching bean ‘userService’ to allow for resolving potential circular references
……
注入userMapper
[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - Autowiring by type from bean name ‘userService’ to bean named ‘userMapper’
……
生成cglib代理类
[org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator] - Creating implicit proxy for bean ‘userService’ with 0 common interceptors and 2 specific interceptors
[org.springframework.aop.framework.CglibAopProxy] - Creating CGLIB proxy: target source is SingletonTargetSource for target object [com.haoyifen.mySSMTemplate.service.UserService@2c06eb2b]
…..
完成创建,此时的userService已经是被cglib代理过的子类的实例
[org.springframework.beans.factory.support.DefaultListableBeanFactory] - Finished creating instance of bean ‘userService’
创建WebApplication子容器
接下来创建WebApplication子容器时,又创建了一次userService,并且没有使用cglib进行代理,所以事务失效。
创建WebApplication
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-mvc.xml]
开始创建userService类
[org.springframework.beans.factory.support.DefaultListableBeanFactory] -* Creating shared instance of singleton bean ‘userService’*
[org.springframework.beans.factory.support.DefaultListableBeanFactory] - Creating instance of bean ‘userService’
[org.springframework.beans.factory.annotation.InjectionMetadata] - Registered injected element on class [com.haoyifen.mySSMTemplate.service.UserService]: AutowiredFieldElement for private com.haoyifen.mySSMTemplate.dao.UserMapper com.haoyifen.mySSMTemplate.service.UserService.userMapper
缓存userService用于其他bean的注入
[org.springframework.beans.factory.support.DefaultListableBeanFactory] - Eagerly caching bean ‘userService’ to allow for resolving potential circular references
[org.springframework.beans.factory.annotation.InjectionMetadata] - Processing injected element of bean ‘userService’: AutowiredFieldElement for private com.haoyifen.mySSMTemplate.dao.UserMapper com.haoyifen.mySSMTemplate.service.UserService.userMapper
[org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean ‘userMapper’
注入userMapper
[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - Autowiring by type from bean name ‘userService’ to bean named ‘userMapper’
完成创建,本来由cglib代理的子类实例被替换成原生的userService实例,自然也就没有了事务功能
[org.springframework.beans.factory.support.DefaultListableBeanFactory] -* Finished creating instance of bean ‘userService’*
我们按照上面的方法将spring-mvc.xml文件更改后,在创建WebApplication时,就不会再重新创建userService了。
Spring重复扫描导致事务失败的解决方案及深入分析的更多相关文章
- 解决spring、springMVC重复扫描导致事务失效的问题
在主容器中(applicationContext.xml),将Controller的注解排除掉 1 2 3 而在springMVC配置文件中将Service注解给去掉 1 2 3 4 因为spring ...
- Spring配置文件xsi:schemaLocation无法解析导致启动失败的解决方案
今天遇到过情况,spring的配置文件在本地读取没有问题,扔到线上服务器运行就报无法解析xml,找了很久问题,发现是因为线上服务器无法上网,导致无法下载相关的xsd文件,没办法不能上网就只有使用本地的 ...
- @Transactional spring事务无效的解决方案
关于@Transactional注解 一般都认为要注意以下三点 1 .在需要事务管理的地方加@Transactional 注解.@Transactional 注解可以被应用于接口定义和接口方法.类定义 ...
- idea创建springboot工程,总出现响应超时问题,或者无法连接http://start.spring.io导致创建失败
问题描述如下: idea创建springboot工程,总出现响应超时问题,或者无法连接http://start.spring.io导致创建失败 从我出现此类问题几次的解决方案 依照解决效率分为一下三种 ...
- [spring transaction],service实现类中非事务方法直接调用自身事务方法导致事务无效的原因
首先,准备service接口,两个 public interface AccountService { public void createAccount(Account account, int t ...
- Spring基于AOP的事务管理
Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...
- Spring之声明式事务
在讲声明式事务之前,先回顾一下基本的编程式事务 编程式事务: //1.获取Connection对象 Connection conn = JDBCUtils.getConnection(); try { ...
- Spring的声明式事务管理
在service类前加上@Transactional,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务. Spring默认情况下会对运行期例外(RunTimeExcep ...
- RabbitMQ 消息顺序、消息幂等、消息重复、消息事务、集群
1. 消息顺序 场景:比如下单操作,下单成功之后,会发布创建订单和扣减库存消息,但扣减库存消息执行会先于创建订单消息,也就说前者执行成功之后,才能执行后者. 不保证完全按照顺序消费,在 MQ 层面支持 ...
随机推荐
- Docker 镜像加速器
Docker 镜像加速器 我们使用Docker的第一步,应该是获取一个官方的镜像,例如mysql.wordpress,基于这些基础镜像我们可以开发自己个性化的应用.我们可以使用Docker命令行工 ...
- Solr记录-solr内核与索引
Solr核心(内核) Solr核心(Core)是Lucene索引的运行实例,包含使用它所需的所有Solr配置文件.我们需要创建一个Solr Core来执行索引和分析等操作. Solr应用程序可以包含一 ...
- HDU 4315 阶梯博弈变形
n个棋子,其中第k个是红色的,每个棋子只能往上爬,而且不能越过.重叠其他棋子,谁将红色棋子移到顶部谁赢. 由于只能往上爬,所以很像阶梯博弈.这题有2个限制,棋子不能重叠,有红棋存在 首先不考虑红色棋, ...
- excel中数字如何自动换行
1. excel中点击单元格右键,选择“设置单元格格式” -- “对齐”选项卡. 2. 先取消“自动换行”,勾选上“缩小字体填充”. 3.再选择“自动换行”即可实现数字的自动换行.
- Sql语句 表中相同的记录(某个字段)只显示一条,按照时间排序显示最大或最小
原始表数据:
- RPM Database
RPM Database RPM 不仅在安装.升级.卸载方面工作出色,而且在查询和验证方面也表现非凡.你很久前安装了一个数据库软件,但现在忘记了它的版本号,也不知道它的说明文档的位置,可以通过 RPM ...
- Linux下利用backtrace追踪函数调用堆栈以及定位段错误【转】
转自:https://www.linuxidc.com/Linux/2012-11/73470p2.htm 通常情况系,程序发生段错误时系统会发送SIGSEGV信号给程序,缺省处理是退出函数.我们可以 ...
- mysql主从复制跳过复制错误【转】
跳过复制错误 mysql因为binlog机制问题,有些时候会出现从库重放sql执行失败的情况,特别是旧的STATEMENT模式最容易出现这种情况(因为函数和存储过程等原因),这也是为什么强调使用mix ...
- eclipse:无法删除不存在的工程
把工程改名后,结果在eclipse里面产生了两个工程,一个原工程,一个是新工程,删除原工程报错, 说工程不存在.这个时候拖动原工程到别的workset中,发现原工程消失了,并找到workspace目录 ...
- 小白学习安全测试(二)——httrack的安装和使用
httrack是一款免费的网站镜像程序,简单理解就是可以在网站结构(网页及一些主要信息文件),下载到本地,可离线浏览,我是按照搭建成功后的console直译过来的 下面说下安装: 我都是在Linux环 ...