Spring transaction internals

前端 未结 3 1654
执笔经年
执笔经年 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
    
    0 讨论(0)
  • 2021-02-04 19:10

    Lets make some basic statements.

    1. A transactional context is an environment where some special properties (database session) are made available to the application runtime which are otherwise not available. A Transaction Context is generally used to scope a transaction.
    2. Spring uses, AOP Proxies and XML metadata to achieve a Declarative transaction management.
    3. Annotations are used to mark the Transaction Propagation behavior of a particular method.
    4. Spring uses Interceptor Mechanism to apply the transaction over the methods.

    Here I am reusing the example give by @stacker above

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

    You can also achieve the same functionality using xml configuration as well. Lets take this as its popular and widely used.

    At the time of deployment

    • Spring framework checks the xml configuration files (famed applicationContext.xml) and depending on the configuration, scans the code for @Transactional annotation (assuming that the configuration is mentioned as annotation based).
    • After this, it generates AOP proxies for methods marked for transaction. In simple terms, these proxies are nothing but wrapper around the concerned methods.
    • Within these wrapper methods, before and after a Transaction Advisor code is also generated depending on the configuration (namely the transaction propagation).
    • Now when these wrapper methods are invoked Transaction Advisor comes into picture before and after the actual method call. .
    • Representing the same in pseudo code for the example above

        ProxyMyClass{   
          MyClass myclass;
          .
          .
          .
          sequence(){
           //Transaction Advisor code (Typically begin/check for transaction)
           myclass.sequence();
           //Transaction Advisor code(Typically rollback/commit)
          }
          .
          .
          .
          }
      

    This is how spring managers the transaction. A slight oversimplification though.

    Now to answer your questions,

    .How does Spring know to commit the database updates upon a successful transaction? Is there some reference to the Spring implementation that does the transaction management?

    Whenever you call a method under transaction, you actually call a proxy which first executes the transaction advisor (which will begin the transaction), then you call the actual business method, once that completes, another transaction advisor executes (which depending on way method returned, will commit or rollback transaction).

    Since we have a hierarchy of transactions: Transaction around the web-request->Transaction with Propagation=RequestNew for Method1->Transaction with Propagation=Required for Method2, how does Spring do the transaction management to ensure the transactions are executed within the proper context with the right order?

    In case of transaction hierarchy, the spring framework generates the Transaction Advisor checks accordingly. For the example you mentioned,

    • for method1 (RequestNew ) Transaction Advsor code (or transaction Advice) would be to create a new transaction always.
    • for method2 (Required ) Transaction Advisor code (or transaction Advice) would be check for existing transaction and use the same if it exists or else create a new transaction.

    There is an image on the spring documentation page which very nicely summarizes these aspects.

    Typical Spring Transaction Management

    Hope this helps.

    0 讨论(0)
  • 2021-02-04 19:11

    Have you read the Spring documentation? Basically AOP is used to manage the transaction. You should also read the AOP documentation. If the AOP documentation is not enough I suggest you go through the code. Stepping through the code in debug mode with break-point would be good.

    0 讨论(0)
提交回复
热议问题