spring transaction related

Keywords: R Language Spring mvc

Java video tutorialhttps://www.xin3721.com/eschool/Javaxin3721/ When I went to the interview before, I was asked how to solve a spring nested transaction. When asked, I was speechless. It happened that the current project just used nested transactions. Distributed transactions are also being attempted. So now I have learned something about nested transactions.

Concept of transaction isolation level

  Transaction propagation - Propagation
REQUIRED: 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.
SUPPORTS: If there is a transaction currently, 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.
MANDATORY: The propagation property enforces the existence of a transaction. If it does not exist, an exception is 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)
REQUIRES_NEW: 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 is true REQUIRED
								    For example: leaders have food, but I don't want it. I bought it myself and ate it myself
	NOT_SUPPORTED: If there is a transaction, suspend the transaction and run the database operation without the transaction
                    For example: leaders have food to eat. I'll give you some. I'm too busy. Put it aside and I won't eat
NEVER: Throw an exception if a transaction currently exists
             For example: the leader has food for you. I don't want to eat. I love work. I throw exceptions
NESTED: If there is a transaction currently, open the sub transaction (nested transaction), and the nested transaction is committed or rolled back independently;
								 If there is no current transaction, the same is true 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.

Attempts in several different situations

There is no transaction isolation level for both parent and child transactions

@Service
public class StuServiceImpl implements StuService {
    @Autowired
    StuMapper stuMapper;

    @Override
    //@Transactional
    public void saveStuChild() {
        saveChildren1();
        int i =1/0;
        saveChildren2();
    }

    public void  saveChildren1(){
        Stu stu = new Stu();
        stu.setAge(11);
        stu.setName("test1");
        stuMapper.insert(stu);
    }

    public void  saveChildren2(){
        Stu stu = new Stu();
        stu.setAge(22);
        stu.setName("test1");
        stuMapper.insert(stu);
    }

    @Override
    //@Transactional(propagation = Propagation.REQUIRED)
    public void saveStuParent() {
        Stu stu = new Stu();
        stu.setAge(30);
        stu.setName("parent");
        stuMapper.insert(stu);
    }
}

Parent transactions

@Service
public class TestTransServiceImpl implements TestTransService {

    @Autowired
    StuService stuService;
    @Override
    public void saveStudent() {
      stuService.saveStuParent();

      stuService.saveStuChild();
    }
}
    @Test
    public void TestNoTransactional(){
        testTransService.saveStudent();
    }

The data before the exception is saved. The data after the exception is not saved

The parent transaction has Required level transactions, and the child transaction has no transactions

@Service
public class TestTransServiceImpl implements TestTransService {

   @Autowired
   StuService stuService;
   @Override
   	@Transactional(propagation = Propagation.REQUIRED)
   public void saveStudent() {
     stuService.saveStuParent();

     stuService.saveStuChild();
   }
}

The stuService code is the same

All transactions are rolled back, and no data is inserted into the database. That is, when the sub transaction has no transaction. Sub methods are also in a transaction. That is, the transaction is passed to the child transaction

The child transaction has Required level transactions, and the parent transaction has no transactions

    @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public void saveStuChild() {
       saveChildren1();
       int i =1/0;
       saveChildren2();
   }


You can see that sub transactions only affect sub transactions. The parent transaction is not affected. Methods without a parent transaction are not affected

The child transaction is a support level transaction, and the parent method has transactions or no transactions

  • When the parent method has no transactions and the child method has no transactions. Annotate @ Transactional(propagation = Propagation.SUPPORTS)
    @Transactional(propagation = Propagation.SUPPORTS)
    public void saveStuChild() {
        saveChildren1();
        int i =1/0;
        saveChildren2();
    }


j is the same as the initial state without transaction

  • The transaction level of the parent method is required. Similarly, as before, data cannot be inserted

The transaction level of the sub method is MANDATORY

Propagation.MANDATORY requires a transaction package, otherwise an exception will be thrown

The transaction level of the sub method is requirements_ NEW

If there is a current transaction, suspend the transaction and create a new transaction for your own use; That is, use your own affairs. If there is no current transaction, the same as REQUIRED. Also use your own transactions

The transaction isolation level of the sub method is NOT_SUPPORTED. That is, transactions are not supported

The parent method without transaction annotation is the same as the case without transaction.

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void saveStuChild() {
        saveChildren1();
        int i =1/0;
        saveChildren2();
    }

The transaction isolation level of the sub method is NEVER,

An exception is thrown if the parent method has a transaction

The transaction isolation level of the sub method is NESTED

If there is a transaction currently, open the sub transaction (nested transaction), and 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.

When the parent method has a transaction and an exception occurs, the child transactions are rolled back together. When an exception occurs in a child transaction. After the server tries.catch, you can choose not to roll back
This mode is used most in daily life. It happened to be used recently.

Posted by jhoop2002 on Thu, 18 Nov 2021 08:41:15 -0800