spring + mybatis 注解 @Transactional失效
生活随笔
收集整理的這篇文章主要介紹了
spring + mybatis 注解 @Transactional失效
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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了...
轉載于:https://www.cnblogs.com/lishuaiqi/p/10485202.html
總結
以上是生活随笔為你收集整理的spring + mybatis 注解 @Transactional失效的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 忍法帖多少级才能获得s
- 下一篇: 100个成语故事大全