开发中遇到的Spring事物小问题

1、在service层开启事物时候,发现事物不一致。

案例:serviceA中有方法a 调用了serviceB中方法b 。。。异常后事物没回滚

跟踪第一步:

跟踪日志发现,每个方法都有was not registered for synchronization because synchronization is not active 这么一日志,明显事物没有被spring管理。

mvc项目中经常还有类似will not be managed by spring问题

解决方案:查找web.xml是否加载了spring。 加载了

    查找spring.xml事物配置,配置了。

    分析项目结构,serviceA包目录为xxx.xxx.service.xxbusiness.ServiceA

    配置文件中确实xxx.xxx.service.*.*(..) 神坑所在啊。

    附下spring官方正则匹配图《面壁思过三百遍,没事多看spring官方文档吧 中文: http://spring.cndocs.tk/index.html 》:

2、过了一会说还有问题,继续看日志(嗯,喜欢日志)

发现大量DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Creating new transaction

向上跟踪,调试代码,最终又发现一个神坑,updatexxx方法调用了很多其他service的方法,但是每个service方法自己都在不断的Creating new transaction

戴上眼镜看代码update方法被某人写成了udpatexxx《面壁思过三百遍,罚抄三百遍update字母》

之后的日志就漂亮多了

一个操作,一个DEBUG [org.springframework.jdbc.datasource.DataSourceTransactionManager] - Creating new transaction

后面的Participating in existing transaction

或者Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6f7b06d] from current transaction

所以,日志很重要

posted on 2017-07-04 11:25  MountainTop  阅读(417)  评论(0编辑  收藏  举报

导航