Relationship Between Spring transaction management and database isolation level (Spring + mysql)

Source: Internet
Author: User
I have written an article titled database isolation level (mysql + Spring) and performance analysis. Many problems in this article are not very professional or logical. Now I want to repeat them, I hope to help you. This part of the experiment through two days is finally to clarify the relationship between them, some of which may be slightly different, but I believe that everyone

I have written an article titled database isolation level (mysql + Spring) and performance analysis. Many problems in this article are not very professional or logical. Now I want to repeat them, I hope to help you. This part of the experiment through two days is finally to clarify the relationship between them, some of which may be slightly different, but I believe that everyone

Zookeeper has previously written an article titled database isolation level (mysql + Spring) and performance analysis. Many problems in this article are not very professional or logical, now, I want to help you.

This part of the experiment has finally clarified the relationship between the two days, some of which may be slightly different, however, I believe that reading it carefully will be of great help to your understanding of this part.
Let's take a general look at my understanding of this issue and summarize it in one sentence:

Databases can control the propagation and isolation of transactions, and Spring further encapsulates the transactions, you can control the transaction Propagation Behavior and isolation level in different projects and operations again.
Note: Spring can not only control transaction propagation behaviors (such as PROPAGATION_REQUIRED), but also control transaction isolation levels (such as ISOLATION_READ_UNCOMMITTED ).

(The following is my understanding. If any defect occurs, please correct it in time)

I will explain it as follows:

For better understanding, let's clarify several knowledge points:

Propagation Behavior of a transaction: in simple words, it refers to whether the transaction is manually committed or automatically committed, when the transaction starts, and when it is committed.
Isolation level of transactions: In simple terms, there are four levels: committed read, committed read, repeated read, and serialized read.

First, let me describe the configuration and experiment methods for transaction Propagation Behavior and isolation level at the database (mysql) level:

Database layer (using command line): in fact, the mySql command line is very simple, and you want to perform the following operations:

// Connect to the database. I am going to be local here, followed by the user name and password. Do not rate the number. If the command is not correct, configure the environment variables. There are many online variables.

1. Run mysql-hlocalhost-uroot-pmysql in cmd.

// Check whether the transaction Propagation Behavior of the local database is manually submitted (0) or automatically submitted (1 ).

2. select @ autocommit;

// If it is set to 0 and you want to set it to manual submission, autocommit is set for this dialog, because if you open another cmd, it is still not changed. If you want to modify the global information, there is a global method on the Internet.

3. set @ autocommit = 0;

// Query a record in the local database. The local database is test1;

4. use test1;

5. select * from task where taskid = 1;

// At the same time, open a new window cmd, connect to the database, and modify this record. I will not write the update statement, or directly modify this record of the database.

// Execute select * from task where taskid = 1 again. The value is not changed. OK, because the database isolation level is repeatable read, because the default isolation level of mysql is repeated read.

// Modify the database isolation level

6. set global transaction isolation level read committed;

// Check the connection and try again.

7. select @ tx_isolation;

// At this time, perform operations 4 and 5 and find that the value has changed. OK. Because the database isolation level has been changed, the reading of different data is repeated.

(I hope you can learn about the above operations on the Internet if you do not understand them. It is very useful. First, you can understand the database isolation level)

Next, let's talk about Spring's secondary encapsulation of transaction Propagation Behavior and isolation level.

Because different projects may be on different databases of one mysql, you can configure the Propagation Behavior and isolation level of the database in the project:

About spring propagation behaviors (PROPAGATION_REQUIRED, PROPAGATION_REQUIRED, etc.), I have mentioned in my article "database isolation level (mysql + Spring) and Performance Analysis", and there are also a lot of relevant information on the Internet, I will not talk about it.

The transaction isolation level of spring is the same as that of the database. If there is a default value, I will not go into detail.

The following describes how to configure spring:



PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED, readOnly
PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED

Take find as an example. You can configure this configuration to control the Propagation Behavior, and to control the transaction isolation level. At this time, even if the database layer is read repeatedly, but it still prevails here, you will find that the results of the two queries in the same transaction are different.

Finally, eliminate a blind spot. The readonly attribute is placed in the Propagation Behavior. Generally, this attribute is classified in books. I also tried it. readonly does not affect the database isolation level, but after configuration, you cannot modify the database in a transaction.

OK. Write so much first. Hope you can correct me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.