Joke Collection Website - Talk about mood - Common database transaction propagation attributes and isolation levels supported by Spring

Common database transaction propagation attributes and isolation levels supported by Spring

So I want to know how much do you know about the common database transaction propagation properties and isolation levels supported by Spring? Do you want to review together: grin:

I like a sentence very much: "Make a living in eight hours, and develop in eight hours."

* * * Cotton: Female:: Computer:

Caption: Come in and have a look at the scenery first. I believe there will be light.

I think everyone knows the essence of database transaction acid (atomicity, consistency, isolation and persistence), so I won't write it here: grin:

As we all know, transactions are used to ensure the integrity of the database, and to ensure that all SQL statements are executed in batches or not.

But what if one method is nested with other methods? Do both the current method and the associated method have transactions, or only some of them have transactions, whose transaction should be used?

Transaction propagation behavior: When a method runs on a method that has opened a transaction, whether the current method uses the original transaction or opens a new transaction.

Set transaction propagation behavior through propagation in @Transaction comments. In ...

There are seven transaction propagation behaviors:

Let's write a small demonstration to make our understanding faster.

Note: The balance field in the account table is set to unsigned (that is, it cannot be negative).

A project is an ordinary Spring project.

It simulates the process of buying books, and the account balance is insufficient, but when you buy more than one book at a time, you pay together.

In it, we will test the difference of transaction propagation behavior to see the change of data.

Initial code:

Mapper layer code

Test 1: Default transaction propagation behavior

We added @Transactional to Void Checkout (IntuserID, List ISBN) and void purchase(int userId, int isbn).

At present, the account 100 yuan, and the prices of two books are 60 and 50 respectively, because our payment process adopts circular purchase. Do you think we will buy one or not?

The answer, of course, is that you can't buy anything, because @Transactional notes that the default transaction propagation attribute is: REQUIRED, that is, the business method needs to run in a transaction. If the method is already in a transaction at runtime, then join the transaction, otherwise create a new transaction for yourself. So void purchase(int userId, int isbn) actually uses the same transaction as the method that called it. Draw a simple picture:

Test 2: Test-> REQUIRES_NEW attribute

Other code hasn't changed, only @ transactional (propagation = propagation. Requirements _ new) has been added to the purchase comment.

REQUIRES_NEW: A business method always initiates a new transaction for itself, whether there is a transaction or not. If the method is already running in a transaction, the original transaction will be suspended and a new transaction will be created. The new transaction will not be completed until the execution of the method is completed, and the original transaction will continue to execute.

Do you think the answer is the same as above? : grinning:

The answer is different. In the first test, we actually used the transaction at checkout, not at purchase, as shown in the figure.

Test 2: Its transaction propagation properties are as follows:

So you can buy a book.

There are still many, the meaning has been explained, not all of them have been tested.

Suppose there are two transactions A and B executing at the same time.

1) dirty read: one transaction reads an uncommitted update from another transaction.

2) Non-repeatable reading: In the same transaction, reading the same data for many times will return different results (for the update operation).

3) Phantom reading: one transaction reads the insert data submitted by another transaction (used for the insert operation of).

Isolation of database transactions: The database system must have the ability to isolate and run various transactions concurrently, so that they will not affect each other and avoid various concurrency problems.

The degree to which a transaction is isolated from other transactions is called isolation level. The database stipulates many transaction isolation levels, and different isolation levels correspond to different interference levels. The higher the isolation level, the better the data consistency, but the weaker the concurrency.

In the code, we can use

The database provides four isolation levels:

Note: Simulate concurrency.

1) Test the default isolation level of mysql:

The test code is very simple, but because I simulate it manually, I have to break the point and start debugging.

When the price of the first book doubles = bookstore cartographer. After the getBookPricebyISBN (ISBN) statement is executed, you should go to mysql to modify the book price, so that you can see the result.

At this time, we will continue to implement it. See what is output.

The final result is still 50-50. Because mysql's default transaction interval level is repeatable, it means that it can be read repeatedly in the same transaction.

Note: Because this is a direct modification of the database, its operation behavior is not desirable, and it is only simulated here. The results are sometimes not necessarily accurate.