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

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 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才是养家糊口的利器。