最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - CompletableFuture.whenComplete() with transactional method calling not working - Stack Overflow

programmeradmin3浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论