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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - Neonuu

解决ollama官方下载过慢的问题(包含windows和linux) 使用java 命令运行包含main方法的class文件时,报 Error: Could not find or load main class Test Java8 list.stream()常用方法 IDEA整合SVN VUE,编辑详情时,列表数据也跟着改变 VUE二级联动,改变一级下拉框,清空二级下拉框 MySQL面试题 JVM原理 Linux服务器安装MariaDB数据库 jar包解压后,修改完配置文件,再还原成jar包 Java获取resources文件夹下properties配置文件 Java中synchronized,wait(),notify() Java中Runnable和Thread的区别(网上部分说法是错误的) Java中如何实现一个接口拥有多个实现类 JPA中getOne与findOne try catch与spring的事务回滚 Redis五种数据类型及应用场景 Java面试题 SQL HAVING用法详解 const,var,let区别
Spring主动触发事务回滚
Neonuu · 2021-04-14 · via 博客园 - Neonuu

前言:

Spring的事务回滚采用@Transactional,当方法中抛出异常时,会触发事务回滚。

如果没有异常,根据业务逻辑,想要主动触发事务回滚,要怎么实现呢?例如,调用一个第三方接口,如果返回值不是200,就要触发事务回滚。这期间并没有代码错误而抛出异常,只是根据接口的返回值来判断是否回滚。

方法一:

throw new RuntimeException(); 

方法二:

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

实测:数据库中没有新增数据

    @Test
    @Transactional
    public void contextLoads3() throws Exception{
        //创建对象
        Game g1 = new Game();
        g1.setName("test");
        g1.setPicUrl("111");
        g1.setType(1);
        //新增到数据库
        gameRepository.save(g1);
        if(true){
            //throw new RuntimeException();//主动抛出异常
            //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//调用事务回滚的方法
        }
    }