惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 聂微东
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
M
MIT News - Artificial intelligence
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
P
Proofpoint News Feed
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理

博客园 - qy1141

Mysql插入Emoji表情出错 安卓开发随记 springmvc + spring + ibatis + mysql Eclipse中配置svn J2EE环境配置与工具使用 SqlServer数据库空间使用情况常用命令 数据库备份与还原 关于数据库优化杂技 windows2008吃尽内存的解决办法 asp.net中http提交数据所遇到的那些坑 在C#中使用消息队列RabbitMQ 重新开博 WCF Security系列(2)--服务器端的安全 WCF Security系列(1)--Security概述 如果为网站生成自签名SSL证书 关于证书 转:最真实的2006年应届毕业生真实薪水 转:如何修复Team Foundation Server Workgroup Edition 不小心删除了所有Team Foundation Licensed Users组内用户问题 转 :TFS(Team Foundation Server)使用经验
spring+mybatis事务不起作用的原因
qy1141 · 2017-04-07 · via 博客园 - qy1141

2017-04-07 11:04  qy1141  阅读(3408)  评论()    收藏  举报

一、场景再现
@Override
@Transactional
public void updateById(String userId,String username) throws Exception {
   sysUserDao.UpdatedById(userId,username);
   if (1==1) {
      throw new Exception("故意抛出测试...");
   }
   sysUserDao.UpdatedById(userId,username+"final");
}

以上就是不起作用的代码,经分析,问题就出在这段代码中。(applicationContext.xml 是标准配置,相信你不会配错)

二、问题分析
上述代码日志打出来后,竟然发现出异常后,事务尽然是commit;

2017-04-07 09:42:16,746 DEBUG-> Creating a new SqlSession
2017-04-07 09:42:16,754 DEBUG-> Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@27c35069]
2017-04-07 09:42:16,766 DEBUG-> JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@592f13f0] will be managed by Spring
2017-04-07 09:42:16,855 DEBUG-> Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@27c35069]
2017-04-07 09:42:16,857 DEBUG-> Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@27c35069]
2017-04-07 09:42:16,857 DEBUG-> Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@27c35069]
2017-04-07 09:42:16,857 DEBUG-> Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@27c35069]

后来查看文档发现这么一段

If no rules are relevant to the exception, it will be treated like DefaultTransactionAttribute (rolling back on runtime exceptions).

理解为:默认情况下,事务遇到运行时异常会回滚;而非运行期异常,事务是不会回滚的。

三、解决办法

解决办法很简单,告诉事务管理器,遇到非运行期异常,也同样需要回滚。

@Override
@Transactional(rollbackFor = {RuntimeException.class,Exception.class})

public void updateById(String userId,String username) throws Exception {
   sysUserDao.UpdatedById(userId,username);
   if (1==1) {
      throw new Exception("故意抛出测试...");
   }
   sysUserDao.UpdatedById(userId,username+"final");
}

题外话:园子里可能很多还是奋战在.net领域的同学,作为一个在一线开发+管理多年经验的老鸟来看,同经验同水平的.net员比java员薪水低20%,级别越高,差距越大,结论就是,如果你想在后端发展,能转java尽早转,不能转创造条件转,.net可以作为兴趣来研究,java才是养家糊口的利器。