Home >Java >javaTutorial >How to solve the transaction label verification problem in springboot project
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.
(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;
(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); }
Do not add transaction labels, the code is as above.
(1) Clear the database first;
(2) Execute tstest request;
It is found that after the insert code line is executed, the data Saved to the database.
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.
(1) Add two labels, one on the startup class method and one on the tstest method.
(2) First clear the data and execute to insert
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.
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!