Let me ask you a question: will this transaction be rolled back?
Program description DD 2021-06-28 23:46:37

The following question comes from our Spring In the technology exchange group , A group friend's question about transaction rollback .

During the discussion , I'm trying to reproduce the problem scenarios raised by my friends , I found another situation that might confuse you .

At that time, the group said the results and reasons , But the scope of wechat group is limited , So write a separate article , Show it to you , By the way , Do you know anything about this .

Problem description

The basic engineering of this problem I used before Spring Boot 2.x In the basic course 《 Use Spring Data JPA visit MySQL》 The case of .

You can go to the warehouse through chapter3-4 Directory access infrastructure :

In this project , Define a name User Entity of :

@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 the length to 5, So that you can get through insert Statement name Super long , Let it throw an exception , So you can test the trigger of the transaction .

In addition, the project also includes Spring Data Jpa Data access object of UserRepository, Used to implement the User Data operations of entities , There is no specific code here .

The problem is coming.

Here the database uses MySQL 5.7, The storage engine is InnoDB, Use the default transaction level .

Let's adjust these four questions :

Question 1 :test1 Will it roll back ?

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

Question two :test2 Will it roll back ?

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

Question 3 :test3 Will it roll back ?( Insert the second sentence name Super long )

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

Question 4 :test4 Will it roll back ?( Insert the second sentence name Super long )

@Transactional
public void test4() {
userRepository.save(new User("BBB", 10));
try {
userRepository.save(new User("123456", 20));
} catch (Exception e) {
log.error(" Exception trapping :", e);
}
}

Leave a message and tell me your answer , Will all four be rolled back ? Search official account : Program the ape DD, reply “ Transaction rollback ”, Get the right answer , See if your judgments are all right ? If you want to collide with more interesting technical souls , reply ” Add group ” Can participate in our high quality communication group , Collision with more technological souls , Grow up together ! More about why transactions don't roll back , Some of them were mentioned in the previous article , Those who are interested can have a look again Why is it added @Transactional annotation , The transaction did not roll back ?.

Please bring the original link to reprint ,thank
Similar articles