mysql - 当其他事务失败时,Spring Boot事务停止自动提交或回滚

标签 mysql hibernate spring-boot spring-transactions

大家,

我正在使用 Mysql 学习 springboot,并尝试专注于事务。我通过以下链接获取指导:

https://github.com/spring-guides/gs-managing-transactions/tree/master/complete

当我执行第一笔交易时,Alice、Bob 和 Carol 被保存。

当我执行第二个事务时,Chris 或 Samuel 被丢弃,因为 Samuel 违反了数据库约束。

我现在想知道的是, 1. 当第二个事务失败时,有没有办法回滚第一个事务? 2. 一切完成后保存所有交易...

最佳答案

是的。
您只需将两个事务声明为 2 个不同的方法,并在第一个事务方法中使用 @Transactional (propagation=Propagation.REQUIRED) 对其进行注释。

有关更多详细信息,请研究事务传播:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html .

关于mysql - 当其他事务失败时,Spring Boot事务停止自动提交或回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46325194/

相关文章:

MySQL - 如何在一个查询中选择多列?

数据库 View 中的 MYSQL 查询

mysql - Sql - IN 运算符行为返回各种结果

java - hibernate 按计数排序连接表并返回 pojo 列表

mysql - Flyway数据导入仅适用于MySQL,不适用于H2

mysql - rails 3;将 'and' 添加到 where 子句

java - Struts 2 和 Hibernate 中的异常处理

java - 如何将 spring-boot 下拉列表获取的数据保存在数据库中?

java - Spring JPA 查询单列结果为通用类型?

spring-boot - Spring Boot and Logging with External Tomcat 只输出启动日志