Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring transaction internals

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

like image 422
sc_ray Avatar asked Dec 23 '11 05:12

sc_ray


People also ask

How do Spring transactions work internally?

Transactions and Proxies. At a high level, Spring creates proxies for all the classes annotated with @Transactional, either on the class or on any of the methods. The proxy allows the framework to inject transactional logic before and after the running method, mainly for starting and committing the transaction.

What does @transactional do in Spring?

The generated proxy object is supplied with a TransactionInterceptor, which is created by Spring. So when the @Transactional method is called from client code, the TransactionInterceptor gets invoked first from the proxy object, which begins the transaction and eventually invokes the method on the target bean.

Why do we use @transactional annotation?

The @Transactional annotation provides the following attributes: value and transactionManager – these attributes can be used to provide a TransactionManager reference to be used when handling the transaction for the annotated block.

What is @transactional in Spring MVC?

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".


2 Answers

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.

like image 165
Santosh Avatar answered Nov 08 '22 23:11

Santosh


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
like image 31
stacker Avatar answered Nov 09 '22 00:11

stacker