Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does transaction suspension work in Spring?

My question is basically the same as is here, but I'm not satisfied with the answer so I'm writing this question.

In Spring Framework manual it is stated that for a PROPAGATION_REQUIRES_NEW the current transaction will be suspended. How is this actually implemented? I know that most databases don't support nested transactions and can have only one transaction running in one connection. This means that you can't just "not use" original transaction and start a new one - before starting new one you must commit or rollback original transaction.

Example:

START TRANSACTION
SELECT ...
UPDATE ...
-- Now we run method with PROPAGATION_REQUIRES_NEW
-- How do we "suspend" transaction so we can start new one?
START TRANSACTION
UPDATE ...
COMMIT
-- We returned from the method, result was commited
-- Now we'd like to "unsuspend" the original transaction so it can be commited/rollbacked, but how?

Or is this possibly implemented using another connection (Session object)? So that we stop using the original connection and create a new one where we can start new transaction?

I am missing here something so obvious that nobody cares to explain it (at least not in Spring docs, Spring in Action, Spring persistence with Hibernate).

Thanks a lot!

like image 575
qbd Avatar asked Oct 22 '13 19:10

qbd


1 Answers

The point of suspending a transaction is to change the current transaction for a thread to a new one. This would NOT line up with the semantics of nested transactions because the new and suspended transactions are completely independent of each other. There is no connection-level API to support suspending transactions so this has to be done by using a different connection. If you are using JTA with Spring, this is done by the JTA transaction manager. If you are using DataSourceTransactionManager, you can look in the code and see that it will be saving off the current connection as a "suspended resource" and grabbing a new connection from the data source for the new transaction.

like image 52
Jess Balint Avatar answered Sep 19 '22 15:09

Jess Balint