Transaction spring affairs propagation mechanisms and isolation levels

Spring transaction propagation behavior

Use Spring transaction must be in-depth understanding of its propagation mechanisms, otherwise they will encounter a variety of unexpected pit, Spring defines seven communication behavior.

public interface TransactionDefinition {
    int PROPAGATION_REQUIRED = 0; int PROPAGATION_SUPPORTS = 1; int PROPAGATION_MANDATORY = 2; int PROPAGATION_REQUIRES_NEW = 3; int PROPAGATION_NOT_SUPPORTED = 4; int PROPAGATION_NEVER = 5; int PROPAGATION_NESTED = 6; } 

The following meanings:

Propagation behavior The official meaning Simple to understand
PROPAGATION_REQUIRED It indicates that the current method must run in a transaction. If an existing transaction is underway, which will run in that transaction, or we need to start a new transaction There is a transaction on the use of existing, not to re-open a
PROPAGATION_SUPPORTS It indicates that the current approach does not require a transactional context, but if there is a transaction is already running, then it can also be run in that transaction in There is a transaction to use an existing, no it will not re-open
PROPAGATION_MANDATORY It indicates that the method must be run in a transaction. If no transaction is currently taking place, it will throw an exception There must be a transaction, the transaction did not throw an exception
PROPAGATION_REQUIRES_NEW It indicates that the current method must run its own affairs inside. A new transaction will be started, and if there is an existing transaction is running, it will be suspended during the operation of this method Open a new transaction, if the current transaction has, pending the current transaction
PROPAGATION_NOT_SUPPORTED It indicates that the method should not be run in a transaction. If an existing transaction in progress, it will be suspended during the operation of the method The transaction is not required, if the current transaction has, pending the current transaction
PROPAGATION_NEVER It indicates that the current approach should not be run in a transaction. If a transaction is in progress, it will throw an exception The transaction is not required, if the current transaction has thrown an exception
PROPAGATION_NESTED Indicates if there is a transaction currently in progress, the method should be run in a nested transaction. Nested transactions can independently commit or roll back the transaction in the package. If the package transaction does not exist, the same behavior as PROPAGATION_REQUIRES Nested transaction, if the transaction is rolled back outside, the nested transaction is rolled back! ! ! When External Affairs submitted, it will be submitted to nesting. Nested transaction rollback does not affect external affairs.

Spring's @Transactional default is PROPAGATION_REQUIRED.

Isolation Levels

Reprinted from the isolation level http://www.javaseo.cn/article/88/

Spring's transaction isolation level eventually to the database processing, Spring does not do special treatment.

    1. Read uncommitted read uncommitted
      company wages, and to 50,000 yuan hit on my account, but the transaction has not been submitted, but I happen to view account and found that wages have been credited into account, is 50,000 yuan a whole, very happy. But unfortunately, the leadership sent the wrong amount discovery wage is 2,000 yuan, then quickly roll back the transaction, the revised amount, the transaction will be submitted to the last my actual salary is only 2,000 yuan, futile.
      Dirty reads are two concurrent transactions, "Transaction A: wages leadership", "Transaction B: I check payroll account" Transaction B reads the data transaction A has not yet submitted.
      When the isolation level is set to Read uncommitted, it may appear dirty read, how to avoid dirty reads, see the next one isolation level.
    2. Read committed to submit read
      I took the payroll card to spend, the system reads the card does have $ 2000, but this time his wife also happens to online transfers, to pay 2000 yuan card to her account, and presented before me affairs, when I charge, the system checks to pay card has no money, a failed charge, very puzzled, obviously Cary money, why ......
      non-repeatable read two concurrent transactions, "transaction a : consumer "," transaction B: when my wife online transfer "transaction a pre-read data, transaction B immediately update the data and submit the transaction, while transaction a reads the data again, the data has changed .
      When the isolation level is set to Read committed, to avoid dirty reads, but may result in non-repeatable read.
    3. Repeatable read Repeatable Read
      When the isolation level is set to Repeatable read time, to avoid non-repeatable read. When I took the card to spend wages, salaries once the system starts to read the card information (ie, the beginning of the transaction), my wife can not modify the record, that is not the transfer at this time.
      Although Repeatable read to avoid non-repeatable read, phantom read but there may arise. For example: my wife works in the banking sector, she often see my credit card records by the bank's internal systems. One day, she was my query to the amount of total consumption month credit card (select sum (amount) from transaction where month = month) is $ 80, but this time just after I eat dinner out at the cash register to pay, consumer $ 1000, that is 1,000 yuan a new consumption record (insert transaction ...), and submit the transaction, then my wife will be spending the month credit card details to print on A4 paper, but found that the total spending of 1080 yuan, my wife was surprised, thinking hallucinations, phantom reads thus produced.
    4. The Serializable (serialization): can prevent dirty reads, non-repeatable read, phantom read occurs.

Guess you like

Origin www.cnblogs.com/zhuyeshen/p/11535156.html