ARTICLE AD BOX
Have a @Transactional method and call an @Async method (since it's another bean, it' should work via proxiy's around advice normally) but it's another bean and no @Transactional annotation there.
So
is sendConfirmation method inside the Transaction?
does @Async's another threadlocal's context affected origin threadlocal's context and get sendConfirmation out of the transaction ?
is @Async removing changes the process or not?
@Service @AllArgsConstructor public class OrderServiceImpl implements OrderService { private final OrderRepository orderRepository; private final NotificationService notificationService; @Override @Transactional public void processOrder(Order order) { order.setStatus(OrderStatus.PROCESSING); orderRepository.save(order); notificationService.sendConfirmation(order); // @Async method order.setStatus(OrderStatus.CONFIRMED); orderRepository.save(order); } } @Service public class NotificationServiceImpl implements NotificationService { @Async @Override public void sendConfirmation(Order order) { // Send email... log.info("Sending confirmation for order: {}", order.getId()); } }3,2217 gold badges39 silver badges77 bronze badges
is sendConfirmation method inside the Transaction?
When the processOrder method calls sendConfirmation, the sendConfirmation method executes in a brand-new and independent thread due to the @Async annotation. This new thread cannot inherit or access the transactional context of the original calling thread, so I think the sendConfirmation method runs outside the transaction.
does @Async's another threadlocal's context affected origin threadlocal's context and get sendConfirmation out of the transaction ?
The threadlocal context of the other thread created by @Async don't affect the context of the original thread; as mentioned above, they are isolated. The issue is that the threadlocal context from the original thread cannot be passed to the new asynchronous thread. This directly causes the sendConfirmation method to be outside the transaction.
New contributor
autext is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Explore related questions
See similar questions with these tags.
