Spring transaction internals

前端 未结 3 1877
执笔经年
执笔经年 2021-02-04 18:13

The situation is as follows:

  1. Method1 has four database update methods in it. The Method1 is annotated using the Spring transaction management semantics.

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-04 18:53

    Controller
    @Transactional
    public void sequence() {
      method1();
      method2();
    }
    
    @Transactional
    void method1() {
    }
    
    @Transactional(propagation=Propagation.REQUIRES_NEW)
    void method2() {
    }
    

    The default propagation is REQUIRED (Support a current transaction, create a new one if none exists.) Therefore m1 will use the Transaction started in the Controller. m2 is annotated as REQUIRES_NEW ( Create a new transaction, suspend the current transaction if one exists.) The order of the transaction is the order you call the transactional methods.

    Controller
    begin tx1
       |--------------------> m1 (uses tx1)
       |
       | begin tx2
       |--------------------> m2 (uses tx2)
       | commit tx2
    commit tx1
    

提交回复
热议问题