Home  >  Article  >  Java  >  How to solve the transaction label verification problem in springboot project

How to solve the transaction label verification problem in springboot project

WBOY
WBOYforward
2023-05-10 23:28:12900browse

1. Problem description

Configuring transactions in the springboot project has been used through tags before. In the last internal project, because it was used in other people’s code, it was found that it did not seem to work (and because of the framework guarantee , don’t worry about it), it is used again in the new startup project, and after verification, it is enough to configure two tags under the springboot project.

2. Solution

2.1 Description

(1) There are actually two tags, one is used in the startup class (@EnableTransactionManagement), and the other is where needed Where to configure (@Transactional);

(2) scheme, I just found a previous test project. One method includes: saving a piece of data to the database and a 1 divided by 0 code, and then look What is the difference between adding or not adding transaction tags;

2.2 Code description

(1)Controller class

@ResponseBody
    @GetMapping("/tstest")
    public String tstest() {
        this.userService.tstest();
        return "SUCCESS";
    }

(2)Service class

public void tstest() {
        //保存
        UserEntity userEntity = new UserEntity();
        userEntity.setType(0);
        userEntity.setOpenid("1111");
        insert(userEntity);
	
				//报错,回滚
        int i = 1/0;
        System.out.println(i);
    }

2.3 Verification 1, there is no transaction

Do not add transaction labels, the code is as above.

(1) Clear the database first;

(2) Execute tstest request;

How to solve the transaction label verification problem in springboot project

It is found that after the insert code line is executed, the data Saved to the database.

How to solve the transaction label verification problem in springboot project

Then the execution is completed (1/0). Although the error code reports an error, the data in the database still exists. The data is not rolled back and consistency is not maintained.

How to solve the transaction label verification problem in springboot project

2.4 Verification 2, there is a transaction

(1) Add two labels, one on the startup class method and one on the tstest method.

How to solve the transaction label verification problem in springboot project

How to solve the transaction label verification problem in springboot project

(2) First clear the data and execute to insert

How to solve the transaction label verification problem in springboot project

It was found that although the insert code line was executed, there was no data in the database at this time. After the execution was completed, the code reported an error, the data was rolled back, and the data was not stored in the database, indicating that the transaction reached the end of the transaction. As a result, the data is rolled back.

How to solve the transaction label verification problem in springboot project

The above is the detailed content of How to solve the transaction label verification problem in springboot project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete