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

推荐订阅源

S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
P
Palo Alto Networks Blog
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
雷峰网
雷峰网
T
Tenable Blog
人人都是产品经理
人人都是产品经理
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
Scott Helme
Scott Helme
SecWiki News
SecWiki News
C
CERT Recently Published Vulnerability Notes
Recorded Future
Recorded Future
I
InfoQ
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
Cloudbric
Cloudbric
C
Check Point Blog
Engineering at Meta
Engineering at Meta
TaoSecurity Blog
TaoSecurity Blog
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
腾讯CDC
量子位
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
Kaspersky official blog
Vercel News
Vercel News
F
Full Disclosure
T
Troy Hunt's Blog
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs

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