Your issue is in the way you are invoking the async 'process_data' method in the 'OrderProcessingService' class. The method is being executed in a separate thread, however, the problem is that your 'process_data' method is not returning anything, and it's not being awaited, which means your program does not wait for the method to finish before proceeding to the next iteration of the loop. As a result, some of your batches might not be saved completely before the loop continues, leading to data loss -
Creating Asynchronous Methods[
^]
Your code should execute using something similar to this -
In your Service Class(OrderProcessingService) -
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class OrderProcessingService {
private final OrderRepository orderRepository;
public OrderProcessingService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
@Transactional
public void processBatch(List<Order> orderList) {
orderRepository.saveAll(orderList);
}
}
In your Rest API Class(DataUploading) -
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
public class DataUploading {
private final OrderProcessingService orderProcessingService;
public DataUploading(OrderProcessingService orderProcessingService) {
this.orderProcessingService = orderProcessingService;
}
@PostMapping("/orders/{hfrcode}")
public void getOrders(@RequestBody List<Order> orders) {
orderProcessingService.processBatch(orders);
}
}
Lastly, in your Desktop App Data Submission Service -
int batchSize = 40000;
for (int i = 0; i < orderList.size(); i += batchSize) {
List<Order> batch = orderList.subList(i, Math.min(i + batchSize, orderList.size()));
orderProcessingService.processBatch(batch);
}
In the '@Transactional' annotation in the 'processBatch' above, the method ensures that the entire batch is saved atomically, rolling back the transaction if any part of the batch fails to save.