I have two different process ( A and B), and A has to start after B, B must not join the A's transaction, B has to wait until A finish its commit.
what propagation should i use ?
Now it is like :
@Transactional
A()
and
@Transactional
B()
Now i use it defult @Transactional, and its not work properly. I think i should use PROPAGATION.
I hope the question is clear. Thanks in advance.
REQUIRED Propagation REQUIRED is the default propagation. Spring checks if there is an active transaction, and if nothing exists, it creates a new one.
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.
@Transactional Annotations should be placed around all operations that are inseparable. Using @Transactional transaction propagation are handled automatically.In this case if another method is called by current method,then that method will have the option of joining the ongoing transaction.
You use @Transcational when concurrent calls on your API can affect each other. Let's say you want to add a Person (you retreive data from somewhere, create a new Person from data and add it to a list of persons).
If B()
in its transactional context calling A()
, then A()
should use REQUIRES_NEW
propagation to has its own independent transaction.
According to spring framework's Data Access Section:
PROPAGATION_REQUIRES_NEW, in contrast to PROPAGATION_REQUIRED, uses a completely independent transaction for each affected transaction scope. In that case, the underlying physical transactions are different and hence can commit or roll back independently, with an outer transaction not affected by an inner transaction’s rollback status.
This photo pretty much tells you why you should use REQUIRES_NEW. What you want is invocation always occurs in a new transaction(TX) context.
Note however: Actual transaction suspension will not work on out-of-box on all transaction managers. If you are using JtaTransactionManager
, you need javax.transaction.TransactionManager
to be made available to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With