Seven propagation behaviors of Spring transactions

1. Required (default)

Meaning: use the current transaction. If there is no transaction, create a new transaction. The sub method must run in a transaction;
If there is a transaction, join the transaction as a whole.
For example: if the leader has no food, I have money, I will buy it myself and eat it myself; If some leaders eat, they will share it with you.
Example: the code is as follows

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void parentMethod(){
    stuService.childrenMethod();
    int a = 1 / 0;
}

//@Transactional(propagation = Propagation.REQUIRED)
public void childrenMethod() {
    Stu stu = new Stu();
    stu.setName("parent");
    stu.setAge(19);
    //stuMapper.insert(stu);
    //int a = 1 / 0;
}

As mentioned above, if the parent method is executed, the parent method will open a thing. When the child method is executed, the child method will be added to the things of the parent method (this is thing propagation). Therefore, if the child method is a single method, there is no need to add things to the child method. If the parent method is abnormal, the child method will roll back.
Application: in practical application, this event propagation behavior is mainly used to add, delete and modify implementation classes.

2,SUPPORTS

Meaning: if there is a transaction, the transaction is used; If there are currently no transactions, no transactions are used.
For example: the leader has no food, and I have no food; Leaders have food, so do I.
Example: the code is as follows

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void parentMethod(){
    stuService.childrenMethod();
    int a = 1 / 0;
}

@Transactional(propagation = Propagation.SUPPORTS)
public void childrenMethod() {
    Stu stu = new Stu();
    stu.setName("jack");
    stu.setAge(19);
    stuMapper.insert(stu);
}

As mentioned above, if the parent method has a transaction and executes the parent method, the parent method will open a thing. When the child method is executed, the child method will be added to the thing of the parent method, and the child method will roll back if the parent method is abnormal. If the parent method has no transaction, when executing the method, both methods are non transaction execution. If the parent method is abnormal, the child method will not roll back.
Application: in practical application, this event propagation behavior is mainly used to implement the query operation of the class.

3,MANDATORY

Meaning: this propagation attribute enforces the existence of a transaction. If it does not exist, an exception will be thrown.
For example: the leader must take care of food. No matter whether there is no food, I don't like it and quit (throw an exception).
Example: the code is as follows

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void parentMethod(){
    stuService.childrenMethod();
}

@Transactional(propagation = Propagation.MANDATORY)
public void childrenMethod() {
    Stu stu = new Stu();
    stu.setName("jack");
    stu.setAge(19);
    stuMapper.insert(stu);
}

As mentioned above, if the parent method has something, execute the parent method, and the parent method will open a thing. When the child method is executed, the child method will be added to the things of the parent method, and the child method will roll back if the parent method is abnormal. If the parent method has nothing, an exception is thrown directly when the method is executed.

4,REQUIRES_NEW

Meaning: if there is a current transaction, suspend the transaction and create a new transaction for your own use; If there is no current transaction, the same as REQUIRED.
For example: leaders have food, but I don't want it. I bought it myself and ate it myself.
Example: the code is as follows

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void parentMethod(){
    stuService.childrenMethod();
    int a = 1 / 0;
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void childrenMethod() {
    Stu stu = new Stu();
    stu.setName("jack");
    stu.setAge(19);
    stuMapper.insert(stu);
    //int a = 1 / 0;
}

As mentioned above, if the parent method has transactions, when running to the child method, it will suspend the transactions of the parent method first, and create a new transaction for its own use. If the parent method is abnormal, the results of the child method will not be rolled back, and data will still be inserted, indicating that the two transactions exist independently. If the child method is abnormal, the results of the parent method will not be rolled back.

5,NOT_SUPPORTED

Meaning: if there is a transaction, suspend the transaction and run the database operation without using the transaction.
For example: the leader has food to eat. I'll give you some. I'm too busy. Put it aside and I won't eat.
Example: the code is as follows

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void parentMethod(){
    stuService.childrenMethod();
    int a = 1 / 0;
}

@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void childrenMethod() {
    Stu stu = new Stu();
    stu.setName("jack");
    stu.setAge(19);
    stuMapper.insert(stu);
    //int a = 1 / 0;
}

As mentioned above, if the parent method has transactions, when running to the child method, it will first suspend the transactions of the parent method and run without transactions. If the parent method has exceptions, the results of the child method will not be rolled back and data will still be inserted. If the child method has exceptions, the results will not be rolled back.

6,NEVER

Meaning: throw an exception if a current transaction exists.
For example: the leader has food for you, I don't want to eat, I love work, and I throw exceptions.

7,NESTED

Meaning: if there is a transaction, the sub transaction (nested transaction) will be opened. The nested transaction is committed or rolled back independently;
If there is no current transaction, the same as REQUIRED.
However, if the primary transaction is committed, it will be committed with the secondary transaction.
If the primary transaction is rolled back, the child transactions are rolled back together. Conversely, if the child transaction is abnormal, the parent transaction can be rolled back or not.
For example: the leader makes a wrong decision, the boss blames, and the leader suffers with his little brother. If the younger brother makes a mistake, the leader can shirk the responsibility.
Example: the code is as follows

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void parentMethod(){
	stuService.saveChildren();
    try {
       	stuService.childrenMethod();
    } catch (Exception e) {
        e.printStackTrace();
    }
    int a = 1 / 0;
}

@Transactional(propagation = Propagation.NESTED)
public void childrenMethod() {
    Stu stu = new Stu();
    stu.setName("jack");
    stu.setAge(19);
    stuMapper.insert(stu);
    //int a = 1 / 0;
}

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void saveChildren() {
    Stu stu1 = new Stu();
    stu1.setName("child-1");
    stu1.setAge(11);
    stuMapper.insert(stu1);
}

As above, try/catch exceptions in the sub method (this method can ensure that the independent transactions in the caller's method will not be rolled back due to the exceptions thrown by the caller)

  1. If the parent method has a transaction, the child transaction will be opened. If the parent method has an exception, the result of the child method will be rolled back and data will not be inserted.
  2. If the parent method has a transaction, the child transaction will be opened. If the child method has an exception and the result of the parent method will not be rolled back, saveChildren() will still insert data.

Above reference: https://blog.csdn.net/soonfly/article/details/70305683
For the actual scenario of transaction propagation behavior, please refer to: https://blog.csdn.net/pml18710973036/article/details/58607148
NESTED transaction NESTED detail reference: https://www.jianshu.com/p/c6d4095f5833

Keywords: Spring Transaction

Added by megaalf on Mon, 10 Jan 2022 15:23:53 +0200