Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Spring @Transactional (propagation = Propagation.SUPPORTS)?

According to the Spring javadoc @Transactional(propagation = Propagation.SUPPORTS)

Support a current transaction, execute non-transactionally if none exists. Analogous to EJB transaction attribute of the same name.

It seems that I can just declare methods non transactionaly and be just done with it so my questions are.

  • What are some situations where SUPPORTS propagation is needed?
  • What is the point of the Supports propagation?

Can anyone give a real world example / scenario where SUPPORTS was actually useful?

like image 845
ams Avatar asked Feb 27 '13 19:02

ams


People also ask

What is @transactional propagation propagation required?

Propagation. REQUIRED is the default setting of a @Transactional annotation. The REQUIRED propagation can be interpreted as follows: If there is no existing physical transaction, then the Spring container will create one.

What is the use of @transactional in Spring?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

Why do we use @transactional in Spring boot?

So when you annotate a method with @Transactional , Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.

Why do we need transaction propagation?

Transaction Propagation indicates if any component or service will or will not participate in transaction and how will it behave if the calling calling component/service already has or does not have a transaction created already.


1 Answers

Easiest example I can think of would be a method that sends some content to a JMS server. If you are in the context of a transaction, you want the message to be coupled to the transaction scope. But if there isn't already a transaction running, why bother calling the transaction server also and starting one just to do a one-off message?

Remember these can be declared on an API as well as an implementation. So even if there isn't much difference for your use case between putting it there and putting nothing there, it still adds value for an API author to specify what operations are able to be included in a transaction, as opposed to operations that perhaps call an external system that does not participate in transactions.

This is of course in a JTA context. The feature doesn't really have much practical use in a system where transactions are limited to resource-local physical database transactions.

like image 83
Affe Avatar answered Oct 25 '22 15:10

Affe