How does transaction suspension work in Spring?

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

The Spring Framework manual states that for PROPAGATION_REQUIRES_NEW the current transaction will be paused. How is this implemented? I know that most databases do not support nested transactions and can only have one transaction running on the same connection. This means that you cannot simply "not use" the original transaction and start a new one - before you start a new one, you must complete or cancel the 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 possible using another connection (Session object)? So that we stop using the original connection and create a new one, where can we start a new transaction?

I miss here so obvious that no one wants to explain it (at least not in Spring docs, Spring in Action, Spring persistence with Hibernate).

Thanks a lot!

+6
source share
1 answer

The pause point of a transaction is to change the current transaction for the stream to a new one. This would not be consistent with the semantics of nested transactions, because new and suspended transactions are completely independent of each other. The connection-level support API does not support transaction suspension, so this must be done using a different connection. If you use JTA with Spring, this is done by the JTA transaction manager. If you use a DataSourceTransactionManager, you can look in the code and see that it will discard the current connection as a โ€œsuspended resourceโ€ and grab a new connection from the data source for a new transaction.

+7
source

Source: https://habr.com/ru/post/956503/


All Articles