The following question stems from a question about transaction rollback raised by a group of friends in our Spring technical exchange group a few days ago.
During the discussion, I tried to reproduce the problem scene raised by the group friends, and found another situation that might confuse everyone.
At that time, I talked about the results and reasons in the group, but the scope of the WeChat group is limited, so I wrote an article separately and showed it to everyone. By the way, I asked you whether you understand this area.
Problem Description
For the basic project of this question, I used the case of "Accessing MySQL with Spring Data JPA" in the previous Spring Boot 2.x basic tutorial.
You can get the basic project chapter3-4
directory in the following warehouse:
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
In this project, define an entity 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;
}
}
Here name
set to a length of 5, so that the name in the insert statement can be too long to make it throw an exception, so that the triggering of the transaction can be tested.
In addition, the project also includes the Spring Data Jpa data access object UserRepository
, which is used to implement data operations on the User entity, so I won’t put the specific code here.
Here comes the problem
Here the database uses MySQL 5.7, the storage engine is InnoDB, and the default transaction level is used.
Let's adjust these four questions below:
Question 1: Will test1 roll back?
@Transactional
public void test1() {
userRepository.save(new User("AAA", 10));
throw new RuntimeException();
}
Question 2: Will test2 roll back?
@Transactional
public void test2() {
userRepository.save(new User("AAA", 10));
try {
throw new RuntimeException();
} catch (Exception e) {
log.error("异常捕获:", e);
}
}
Question 3: Will test3 roll back? (The second sentence inserts name is too long)
@Transactional
public void test3() {
userRepository.save(new User("BBB", 10));
userRepository.save(new User("123456", 20));
}
Question 4: Will test4 roll back? (Insert the name in the second sentence is too long)
@Transactional
public void test4() {
userRepository.save(new User("BBB", 10));
try {
userRepository.save(new User("123456", 20));
} catch (Exception e) {
log.error("异常捕获:", e);
}
}
Leave a message and tell me your answer. Will these four rollbacks?
Prompt test4 is special! Give some time to think about it first, don't go away, remember to follow me, and the answer and reason will be announced in the next article! If you are really hungry and thirsty, you can follow the official account: Program Ape DD, reply "transaction rollback" to get the correct answer. See if your judgment is correct?
If you question the answers given, it is strongly recommended to download the case of the article, and then write a few lines of code to try these kinds of situations! If you still can't believe it, let's debug it!
PS title is a bit biased, maybe you don't write it like this everyday, but I hope this unexpected result can guide you to follow the source code to find out!
Welcome to pay attention to my public account: Program Ape DD, share knowledge and thoughts that can’t be seen elsewhere
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。