码迷,mamicode.com
首页 > 编程语言 > 详细

spring + mybatis 注解 @Transactional失效

时间:2019-03-06 19:24:06      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:问题   def   lte   size   没有   res   clu   bec   framework   

1.问题

在使用@Transactional注解管理事务的时候会出现很多错误,比如:

***  was not registered for synchronization because synchronization is not active
或者
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ca5cba7]
JDBC Connection [com.mysql.jdbc.JDBC4Connection@79436e0a] will not be managed by Spring

总之就是事务没有被spring管理,注解@Transactional失效.

2.原因:

重复扫描包的问题. 因为springmvc的配置文件已经扫描了service和controller注解,而spring的配置文件又扫描了一遍,所以会出错. 

spring 通过 cglib 生成了带有事务功能的代理类.

但是spring mvc 在扫描一遍又重新生成了 service 层的不带事务功能的代理类,把之前的代理类给覆盖掉了,

所以会导致事务失效.

因此解决就是把springmvc扫描service给过滤掉就可以了,

3.解决:

让springmvc的配置文件只扫描controller

    <context:component-scan base-package="com.fyq" >
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    

通过 filter 把service注解给过滤掉.

同理 spring 的配置文件只扫描service层,把controller给过滤掉.

    <context:component-scan base-package="com.fyq" >
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

 

 

该用spring-boot了...

spring + mybatis 注解 @Transactional失效

标签:问题   def   lte   size   没有   res   clu   bec   framework   

原文地址:https://www.cnblogs.com/lishuaiqi/p/10485202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!