I am using Spring boot 2.4.4 with Webflux. My use case is to rollback trnasaction on some condition.
Exposing a REST end point. On getting a request,
- Insert into order table
- Insert into order test table
- Call remote service to notify order
- Rollback order table and order test table entry on receiving error from the remove service.
Code
@Transactional
public Mono<ServerResponse> submitOrder(final ServerRequest request) {
return request.bodyToMono(OrderDto.class).flatMap(orderDto -> {
return orderRepository.save(toOrder(orderDto)).flatMap(order -> {
return orderTestRepository
.saveAll(toOrderTestList(orderDto.getTestList())).collectList()
.flatMap(labTestRes -> {
// Need to rollback transaction if the below service call is not success
return removeService.submitOrder(orderDto, labOrder.getOrderId())
.flatMap(orderNum -> {
return noContent().build();
});
});
});
});
}