Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use @Transactional's propagation=Proagation.REQUIRES_NEW?

My question is simple and nearly already stated in the title: Is there a specific pattern/ use-case where one would say that @Transactional(propagation=Proagation.REQUIRES_NEW) instead of the default propagation should be used?

Thanks !

like image 868
Erik Avatar asked Jan 20 '23 05:01

Erik


2 Answers

Writing an audit trail. You want to record the fact that someone tried to do something irrespective of whether the main transaction succeed or failed (and hence got rolled back).

like image 57
djna Avatar answered Jan 21 '23 19:01

djna


Specific uses include scenarios where you need to perform some work in a method, and commit it before continuing the existing transaction.

"Auditing" (and not logging) of method invocations, for instance, is a scenario where you would want to commit the audit entry irrespective of whether the existing business transaction will commit or rollback in the end.

Another scenario would be the case where you have several business transactions that are being invoked from a facade, and each such invocation must commit it's work before returning control to the facade. This scenario is quite rare, but you might find this in a workflow system that creates it's own transaction (where entries are recorded on the progress of the workflow), and invocations on other components residing in the same application, must occur in separate transactions.

like image 25
Vineet Reynolds Avatar answered Jan 21 '23 20:01

Vineet Reynolds