Contents of this article:

What is a transaction

A transaction is an indivisible operation

What are the four characteristics of transactions

atomic

A non-separable operation, in which all operations within a transaction succeed or fail

Isolation,

One transaction cannot be disturbed by another

persistence

The transaction commits, and the operation on the data is permanent

consistency

Transactions must change data from one consistent state to another

The isolation level of the transaction

Read committed data

Unrepeatable reads and phantom reads occur

Read uncommitted data

Dirty read, non-repeatable read (not used almost any more)

Repeatable read (default is repeatable read)

Phantom reading will occur

serialization

What problems can occur in the concurrent state of a transaction

Dirty read

A transaction reads data that another transaction has not committed to update

Example: [TRANSACTION B commits data, transaction A queries, transaction B rolls back, transaction A queries again, transaction A finds two query results are inconsistent]

Phantom read

One transaction reads inserted/deleted data that another transaction has committed

Example: [A transaction query,B transaction insert or delete,A transaction query again found two query data inconsistent results]

Do not write and read repeatedly

One transaction reads the update data committed by another transaction

Example: [A transaction query,B transaction update,A transaction query results are inconsistent]

Lost update

The cancellation of one transaction overwrites the update commit data of another transaction

Example: transaction A updates the data, transaction B updates the data after transaction A, transaction A rolls back, and then transaction B rolls back the data.

Covering the update

The commit of one transaction overwrites the committed update data of another transaction

Example: [transaction A updates the data, and transaction B also updates the data, and transaction A finds that the data it updated is overwritten]

A few things about transactions in Spring

What propagation behavior transactions have in Spring

Transactional(propagation=Propagation.REQUIRED)

If there is a transaction, join it, if not, create a new one (default)

@Transactional(propagation=Propagation.NOT_SUPPORTED)

The container does not open transactions for this method

@Transactional(propagation=Propagation.REQUIRES_NEW)

Regardless of whether a transaction exists, a new transaction is created, the original transaction is suspended, the new execution completes, and the old transaction continues

@Transactional(propagation=Propagation.MANDATORY)

Must be executed within an existing transaction, otherwise an exception is thrown

@Transactional(propagation=Propagation.NEVER)

Must be executed in a transaction that does not exist, otherwise an exception is raised (as opposed to Propagation.Mandatory)

@Transactional(propagation=Propagation.SUPPORTS)>

If another bean calls this method and declares a transaction in another bean, use a transaction. If the other bean does not declare a transaction, then no transaction is required.