springmvc + mybatise 一个service 处理处理多个mapper 事物不回滚
2016-04-19 11:52
393 查看
问题出现情况:
声明式事物@Transactional
在一个service调用了两个不同mapper 先后update两个数据,人为模拟后一个mapper异常,发现第一个mapper会提交数据。
网上有两种解决方法 第一个 方法 ,在@Transactional 调整事物传播方式从默认requared改为NESTED,并加入rollbackFor=Exception.class,修改后,无效。检查日志,发现当第一个mapper在执行完update时会按如下顺序执行:
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d0d75] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==> Preparing: update ss_user s_user set s_user.certsn=?,s_user.deptno=?,s_user.userName=?,s_user.freeze=? where id=?
DEBUG - ==> Parameters: null, 1(String), 要12(String), true(Boolean), 01ed2b6bee29458a88c0d54b9ab3b63a(String)
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d0d75]
DEBUG - Returning JDBC Connection to DataSource
此时,已经Returning JDBC Connection to DataSource
再执行第二个mapper的update会报异常,但是没有回滚第一次操作。
网上第二个解决办法 原文来着这里:
http://blog.csdn.net/will_awoke/article/details/12002705
主要意思是,因为web.xml中的设置,spring会默认加载bean*.xl中的容器作为父容器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean*.xml</param-value>
</context-param>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
而在bean.xml中我设置了
<context:component-scan base-package="org.ezca.signsserver">
</context:component-scan>
父容器中的扫描到@trancction的bean是具有事物管理能力的,而springmvc.xml 中再次用到
<context:component-scan base-package="org.ezca" >
</context:component-scan>
而此时,装配出来的bean是不具有事物管理能力的
,于是,子容器的bean将父容器的bean覆盖。
所以解决办法为在springmvc的扫描中加入:
<context:component-scan base-package="org.ezca" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
将可能出现@tranction的bean过滤掉.
修改后运行效果如下:
第一个mapper
DEBUG - Creating a new SqlSession
DEBUG - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] will be managed by Spring
DEBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==> Preparing: update ss_user s_user set s_user.certsn=?,s_user.deptno=?,s_user.userName=?,s_user.freeze=? where id=?
DEBUG - ==> Parameters: null, 1(String), 要13(String), true(Boolean), 01ed2b6bee29458a88c0d54b9ab3b63a(String)
DEBUG - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
第二个mapper异常时:
EBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==> Preparing: insert into ss_user_cert(id,alias,crlUrl,destory,encode,invalid,oid,publicKey,sn,subject,time,type,notBefore,notafter) values(?,'','http://www.ezca.org/crl/crl8.crl',?,?,?,?,?,?,?,?,1,?,?)
DEBUG - Unable to translate SQLException with Error code '17004', will now try the fallback translator
DEBUG - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Initiating transaction rollback
DEBUG - Rolling back JDBC transaction on Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - Transaction synchronization rolling back SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Releasing JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] after transaction
DEBUG - Returning JDBC Connection to DataSource
关键词 :Rolling back JDBC transaction on Connection
于是,回滚完成。
问题出现情况:
声明式事物@Transactional
在一个service调用了两个不同mapper 先后update两个数据,人为模拟后一个mapper异常,发现第一个mapper会提交数据。
网上有两种解决方法 第一个 方法 ,在@Transactional 调整事物传播方式从默认requared改为NESTED,并加入rollbackFor=Exception.class,修改后,无效。检查日志,发现当第一个mapper在执行完update时会按如下顺序执行:
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d0d75] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==> Preparing: update ss_user s_user set s_user.certsn=?,s_user.deptno=?,s_user.userName=?,s_user.freeze=? where id=?
DEBUG - ==> Parameters: null, 1(String), 要12(String), true(Boolean), 01ed2b6bee29458a88c0d54b9ab3b63a(String)
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d0d75]
DEBUG - Returning JDBC Connection to DataSource
此时,已经Returning JDBC Connection to DataSource
再执行第二个mapper的update会报异常,但是没有回滚第一次操作。
网上第二个解决办法 原文来着这里:
http://blog.csdn.net/will_awoke/article/details/12002705
主要意思是,因为web.xml中的设置,spring会默认加载bean*.xl中的容器作为父容器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean*.xml</param-value>
</context-param>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
而在bean.xml中我设置了
<context:component-scan base-package="org.ezca.signsserver">
</context:component-scan>
父容器中的扫描到@trancction的bean是具有事物管理能力的,而springmvc.xml 中再次用到
<context:component-scan base-package="org.ezca" >
</context:component-scan>
而此时,装配出来的bean是不具有事物管理能力的
,于是,子容器的bean将父容器的bean覆盖。
所以解决办法为在springmvc的扫描中加入:
<context:component-scan base-package="org.ezca" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
将可能出现@tranction的bean过滤掉.
修改后运行效果如下:
第一个mapper
DEBUG - Creating a new SqlSession
DEBUG - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] will be managed by Spring
DEBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==> Preparing: update ss_user s_user set s_user.certsn=?,s_user.deptno=?,s_user.userName=?,s_user.freeze=? where id=?
DEBUG - ==> Parameters: null, 1(String), 要13(String), true(Boolean), 01ed2b6bee29458a88c0d54b9ab3b63a(String)
DEBUG - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
第二个mapper异常时:
EBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==> Preparing: insert into ss_user_cert(id,alias,crlUrl,destory,encode,invalid,oid,publicKey,sn,subject,time,type,notBefore,notafter) values(?,'','http://www.ezca.org/crl/crl8.crl',?,?,?,?,?,?,?,?,1,?,?)
DEBUG - Unable to translate SQLException with Error code '17004', will now try the fallback translator
DEBUG - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Initiating transaction rollback
DEBUG - Rolling back JDBC transaction on Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - Transaction synchronization rolling back SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Releasing JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] after transaction
DEBUG - Returning JDBC Connection to DataSource
关键词 :Rolling back JDBC transaction on Connection
于是,回滚完成。
相关文章推荐
- iOS 栈和堆的区别
- iOS开发者有价值的工具集
- cocos studio 3.10点击在windows平台运行报错 在windows平台运行 返回值1
- iOS开发——界面跳转方法总结
- 基于 Facebook Redex 实现 Android APK 的压缩和优化
- 禅与 Objective-C 编程艺术
- Android源码抽象工厂---IPolicy
- Android按钮单击事件的四种常用写法总结
- iOS应用跳转问题之:应用跳转
- objectToArray2stdClass Object.php
- Hbuilder MUI webview 创建页面self.append(sub)与sub.show()区别
- java ObjectOutputStream
- android的引用库类
- Swift -- 2.基本数据类型
- Android项目中打jar包 和 使用
- Android滑动切换页面Tab文字颜色发生渐变效果
- Android 垃圾回收,用软引用建立缓存
- android studio利用命令行打包生成apk
- Github上的iOS开源项目1
- iOS CoreImage进行人脸检测