springtransactions

What does suspending a transaction mean?


If we are using Propagation.REQUIRES_NEW then it suspends the existing transaction and creates a new transaction. So what does it mean to suspend a transaction? What happens to the suspended transaction? What exactly happens behind the scenes?

Edit: What happens to the resources held by suspended transaction?


Solution

  • First of all the question here is a duplication of the thread: How does transaction suspension work in Spring?. However, I will try to answer this in a different way.

    To understand the working of Spring @Transaction API, we must look inside the transaction propagation mechanism.

    Spring managed transaction has physical and logical transactions depending upon the configuration.enter image description here

    PROPAGATION_REQUIRES_NEW uses a completely independent transaction for each affected transaction scope. The underlying physical transactions are different and hence can commit or roll back independently. Here the outer transaction is not affected by an inner transaction’s rollback status.

    When a transaction is suspended, it waits until it can pick up where it left off. This means, the changes that happened while the transaction is suspended are NOT part of the same atomic unit. In other words, the things that happened while the transaction is suspended won’t be rolled back if the suspended transaction (after it comes back to life) fails to commit.

    Spring transaction does not expose any API for developers to control this directly, other than the transaction configurations. However if you are using JTA to manage transaction then you can call the suspend and resume methods as below:

    Transaction tobj = TransactionManager.suspend();
    ..
    TransactionManager.resume(tobj);
    

    I hope this helps you!