Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Usage of MYSQL nested transactions and Spring transaction Propagation

Shulou Source: shulou.com Published: 2022-06-02 08:30:05 10月05日 Update

This article mainly explains "the use of MYSQL nested transactions and Spring transaction propagation". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of MYSQL nested transactions and Spring transaction propagation".

Abstract

The savepoint keyword enables nested transactions. Combined with the savepoint keyword, it is easier to understand the transaction propagation of spring.

Drop table tbot create table t (an int, primary key (a)); start the transaction

Mysql transactions commit automatically by default. Turn off transaction auto-commit with the following command. (the mysql transaction feature is based on the same link, and different links have no transaction relationship, so when testing, make sure it is the same window.)

Set @ @ autocommit = 0bot select @ @ autocommit; local transaction, external transactions are committed normally-- open transaction begin;insert into t (a) values (1);-- SAVEPOINT Sten insert into t (a) values (2); RELEASE SAVEPOINT Sitcommit

Query results, 1 and 2 have been inserted successfully.

Select * from t

Local transaction rollback, external transaction commit begin;insert into t (a) values (1); SAVEPOINT s commit insert into t (a) values (2); ROLLBACK to SAVEPOINT s commit

Here, the local transaction is rolled back, the overall transaction is committed, and the query result is only 1 inserted successfully, 2 is not inserted, indicating that the external transaction has been committed and the local transaction has been rolled back.

Select * from t

Local transaction commit, external transaction rollback begin;insert into t (a) values (1); SAVEPOINT s ten insert into t (a) values (2); RELEASE SAVEPOINT s ten rollback

The global transaction is rolled back, the whole transaction is rolled back, and neither 1 nor 2 is inserted successfully.

Select * from t

Local transaction rollback, external transaction rollback

It is obvious that neither 1 nor 2 can be inserted successfully.

Thank you for your reading, the above is the content of "the usage of MYSQL nested transactions and Spring transaction propagation". After the study of this article, I believe you have a deeper understanding of the use of MYSQL nested transactions and Spring transaction propagation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Transaction local propagation success learning key keyword content result link query different obvious global only command that is idea situation summary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Redmi OPPO Reno Shulou Technology