What are the consequences of calling a method annotated with @Transactional with Propagation.NEVER in Spring?
sobes.tech AI
Answer from AI
If you call a method annotated with @Transactional with the parameter propagation = Propagation.NEVER in Spring, Spring will check that there is no active transaction in the current execution thread. If a transaction already exists, an IllegalTransactionStateException will be thrown.
This means that a method with Propagation.NEVER disallows execution within a transaction. This approach is used when you need to ensure that the method executes outside of a transactional context, for example, for operations that should not be part of a transaction or for calling external services that do not support transactions.
Example:
@Transactional(propagation = Propagation.NEVER)
public void nonTransactionalMethod() {
// code that should not be executed within a transaction
}
Calling this method from another method that is already within a transaction will result in an error.