My server runs on a RestFul architecture with Spring-boot, using spring-mvc, hibernate and oauth2. No HTTP Session, stateless. I need to upload a file a do a parsing of this file on server-side to extract data and store them in my database. This processing may take a while so I want to follow the progress of this treatment to display a progress bar in my webapp. The parsing of the file is a defined as a transactional spring service. Withing this service, I update some counters and I want to store these counters in my database. Then I have a rest service to fetch these counters periodically from my webapp through a polling mechanism. My problem : as my parsing treatment is a transactional service, everything is commited in database only at the end of the processing. So my counters are also commited at the end of the processing. So my REST service to retrieve my counters is useless as it always returns empty... --> How can I commit my counters to my database while maintaining a transactional service for the whole service ? I thought it would have something to deal with spring transactions propagation, I tried to define some methods with Propagation.REQUIRES_NEW for the update of my counters but no way, my counters are only persisted at the end of the service execution... Maybe my approach is not the right one, feel free to suggest me alternative ways !
Thanks
So here is my code. All the methods are declared in a same class annotated with @Service
@Service
public class AuditService {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createNewResult(Long transactionId, Long auditTotal) {
AuditLaboImportResult auditResult = new AuditLaboImportResult();
auditResult.setTransactionId(transactionId);
auditResult.setAuditsTotal(auditTotal);
auditResult.setAuditsTraites(0L);
auditLaboImportResultRepository.save(auditResult);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateUploadResult(Long transactionId, Long auditTraites) {
AuditLaboImportResult auditResult = auditLaboImportResultRepository.findByTransactionId(transactionId);
auditResult.setAuditsTraites(auditTraites);
auditLaboImportResultRepository.save(auditResult);
}
@Transactional
public List<Audit> uploadFiles(Long transactionId, MultipartFile fileEHT, MultipartFile fileEHC) {
....
createNewResult(transactionId, new Long(audits.size()));
....
for (Audit audit : audits) {
....
updateUploadResult(transactionId, auditsTraites);
....
}
}
}