18

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

update

what happens to the resource held by suspended transaction?

Community
  • 1
  • 1
eatSleepCode
  • 4,427
  • 7
  • 44
  • 93

1 Answers1

13

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!

Community
  • 1
  • 1
SyntaX
  • 2,090
  • 17
  • 30
  • 3
    when a transaction is in suspended state does it holds lock on the resources for that duration? – eatSleepCode Nov 16 '15 at 08:55
  • `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.` what does this mean? does it mean changes made by suspended transaction wont be rolled back though transaction doesn't commits? – eatSleepCode Nov 16 '15 at 08:56
  • When a transaction is suspended, the the resources related to the suspended transaction are free up. When the application’s transaction context is resumed, it gets its resources back. Say the `transaction` associated to `method1` is suspended and the `transaction` associated to `method2` is resumed. Now say `method2` execution is completed successfully and the `transaction` associated with `method2` is committed. Now transaction for `method1` resumes, I meant even if `method1` fails now, this will not rollback the changes made in `method2`. – SyntaX Nov 16 '15 at 09:09
  • 3
    suppose if I have made an update in method1 and now the transaction is suspended, will it keep the lock on the updated row? and how does it manages this if the resources are freed up? – eatSleepCode Nov 16 '15 at 09:31
  • *The lock will be released when a transaction is suspended*. If needed it has to acquire later when it is resumed. That is why you have to be careful at choosing the right configuration. My suggestion is you create a simple example and play around, that will definitely give you more clarity. – SyntaX Nov 16 '15 at 09:47
  • Do you still have query? – SyntaX Nov 16 '15 at 12:12
  • that answers my concern – eatSleepCode Nov 16 '15 at 12:44
  • 1
    @SyntaX where can I find this information that resources (locks?) are released when a transaction is suspended? AFAIK, suspension is just a Spring mechanism to change the active tx associated with the current thread. – john16384 Mar 09 '21 at 13:28
  • 3
    @SyntaX - I have played around with this pretty extensively with test applications, and from what I have seen (at least with MS SQL Server) the transaction retains its locks on any rows and/or tables. The only thing suspending seems to do is make it so that the transaction is no longer associated with the currently running thread. – matt forsythe Apr 03 '21 at 02:45