I have a class in my spring boot 3 application that sends a Kafka message and updates the notification status and report after the message is sent:
public class SomeSender {
KafkaTemplate<?, ?> kafkaTemplate;
NotificationService notificationService;
NotificationReportService notificationReportService;
public void send(Notification notification) {
kafkaTemplate.send(notification).whenComplete((success, error) -> {
updateNotificationStatus(notification);
notificationReportService.createReport(notification);
});
}
private void updateNotificationStatus(Notification notification) {
// some logic
notificationService.update(notification);
}
}
Both notificationService.update(notification) and notificationReportService.createReport(notification) are marked with @Transactional.
The problem is that the notification status does not update, and some reports are partially saved or not saved at all. It seems like the transactional methods are not working correctly inside whenComplete().
What could be the cause of this issue, and how can I ensure that @Transactional methods work properly in this case?