5

我来出个题:这个事务会不会回滚?

 3 years ago
source link: https://blog.didispace.com/will-this-transcation-rollback/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

我来出个题:这个事务会不会回滚?

下面这个问题源于前几日在我们的Spring技术交流群里,一个群友提出的关于事务回滚的疑问。

在讨论过程中,我尝试去复现群友提出的问题场景,发现了另外一个可能让大家会迷惑的情况。

当时在群里说了结果和原因,但微信群范围有限,所以单独写篇文章,拿出来给大家看看,顺便考考大家,对这块是否了解。

这个问题的基础工程我用了之前Spring Boot 2.x基础教程中《使用Spring Data JPA访问MySQL》的案例。

你可以通过下面仓库中的chapter3-4目录获取基础工程:

在这个工程中,定义一个名为User的实体:

@Entity
@Data
@NoArgsConstructor
public class User {

@Id
@GeneratedValue
private Long id;

@Size(max = 5)
private String name;
@Max(50)
private Integer age;

public User(String name, Integer age) {
this.name = name;
this.age = age;
}

}

这里name设置了长度为5,这样可以通过insert语句中的name超长,让其抛出异常,从而可以测试事务的触发。

另外工程中还包含了Spring Data Jpa的数据访问对象UserRepository,用来实现对User实体的数据操作,这里就不放具体代码了。

这里数据库采用MySQL 5.7,存储引擎为InnoDB,使用默认事务级别。

下面来调整下这四个问题吧:

问题一:test1会不会回滚?

@Transactional
public void test1() {
userRepository.save(new User("AAA", 10));
throw new Exception("");
}

问题二:test2会不会回滚?

@Transactional
public void test2() {
userRepository.save(new User("AAA", 10));
try {
throw new Exception("");
} catch (Exception e) {
log.error("异常捕获:", e);
}
}

问题三:test3会不会回滚?(第二句插入name超长)

@Transactional
public void test3() {
userRepository.save(new User("AAA", 10));
userRepository.save(new User("1234567890", 20));
}

问题四:test4会不会回滚?(第二句插入name超长)

@Transactional
public void test4() {
userRepository.save(new User("AAA", 10));
try {
userRepository.save(new User("1234567890", 20));
} catch (Exception e) {
log.error("异常捕获:", e);
}
}

留言说说你的答案吧,这四个都会不会回滚?搜索公众号:程序猿DD,回复“事务回滚”,获取正确答案,看看你的判断都对吗?如果你想与更多有趣的技术灵魂碰撞,回复”加群”就可参与我们的高质量交流群,与更多技术灵魂碰撞,一起成长哦!更多关于事务为什么不回滚的情况,在之前的这篇老文中也有提到一些,有兴趣的可以再看看 为什么加了@Transactional注解,事务没有回滚?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK