Spring transaction internals

左心房为你撑大大i 提交于 2019-12-03 07:50:47

问题


The situation is as follows:

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

  2. Method2 has a database read method in it and it is invoked after Method1 has finished executing all its database updates. Method2 is also annotated using the Spring transaction semantics.

  3. There's a web request that comes in, the controller intercepts the request and invokes method1 and then method2.

  4. A transaction is wrapped around the web-request as well.

What I am interested in knowing is:

1.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?

2.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 short, it will be great to get a play by play account of how Spring performs the transaction management in all its grittiest details or a reference to documentation that doesn't simply hand-wave an explanation centered around JTA or some other acronym.

Thanks


回答1:


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.

Hope this helps.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/8612486/spring-transaction-internals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!