A brief introduction to the common database transaction propagation attributes and transaction isolation levels supported by Spring

The properties of the transaction:

Familiarity with affairs:

        1) propagation: used to set the propagation behavior of the transaction

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

             -Propagation.REQUIRED: default value, use the original transaction

             -Propagation.REQUIRES_NEW: Suspend the original transaction and start a new transaction

 

         2) isolation: used to set the isolation level of the transaction

           Isolation level of the transaction : the degree of isolation of a transaction from other transactions is called the isolation level. The higher the isolation level, the better the data consistency, but the weaker the concurrency

 

The background is to buy books and roll back the database when the money is not enough

@Autowired

private BookService bookService;



@Transaction

public void checkOut(){

		bookService.purchase();

}



@Transaction(propagation=Propagation.REQUIRES_NEW,isolation=Isolation.REPEATABLE_READ)

public void purchase(){}

There is also a checkOut transaction outside the purchase

1) If the purchase transaction is REQUIRED propagation behavior:

When the purchase() method of bookService is called by another transaction method checkOut(), it will run in the existing transaction by default. The default propagation behavior is REQUIRED. Therefore, there is only one transaction within the start and end boundary of the checkOut() method. This transaction is only submitted at the end of the checkOut() method. As a result, the user cannot buy a book

2) REQUIRES_NEW propagation behavior

This method must start a new transaction and run within its own transaction. If there is a transaction running, it should be suspended first. The following result is that the first book is enough money to buy, and the second book is not enough money to buy. Rather than the situation like 1), when the second copy is not enough money, it will be rolled back, and the first copy cannot be bought.

 

Transaction isolation level

1) Read uncommitted: READ UNCOMMITTED

Allow Transaction01 to read the uncommitted changes of Transaction02

 

2) Read has been submitted: READ COMMITTED ( 开发时通常使用的隔离级别)

Require Transaction01 to only read the changes submitted by Transaction02

 

3) Repeatable read: REPERTABLE READ

Ensure that Transaction01 can read the same value from a field multiple times, that is, during the execution of Transaction01, other transactions are forbidden to update this field

 

4) Serialization: SERIALIZABLE

Ensure that Transaction01 can read the same row from a table multiple times. During the execution of Transaction01, prohibit other transactions from adding, updating, and deleting operations on this table . Can avoid task concurrency problems, but the performance is very low

 

Database transaction concurrency problem

Dirty read: The updated value is read, and the value read before the rollback is wrong

Non-repeatable read: read a value, read it again after modification, the two reads are inconsistent (but the requirement is to be consistent)

Phantom read: read a part, insert a new one into the table, flick it again, some extra rows

 

Guess you like

Origin blog.csdn.net/di_ko/article/details/114920764