I am developing Spring-boot application, there is file upload feature from service layer Here is code snippet-
@Transactional(rollbackOn = AmazonServiceException.class)
@Override
public Post createPost(PostEntity postEntity, MultipartFile attachments) {
uploadOnS3(postEntity.getPostMedia(), attachments); // push into s3 bucket
.....
....
postRespository.save(postEntity);//store into application databse
}
Problem Scenario:
Lets say file uploaded successfully into s3 bucket and its return file url
exception occurred after next step and transaction has been rolled backed
Since file already has been uploaded, so application wouldn't have any information
What I have been tried so far:
I have tied to insert local db things before s3 bucket upload but I didn't get any callback like local db insertion has been completed now we should start s3 task.
At the the same time I have to also sent notification to client regarding status
Does spring provide any mechanism by which we can know local db insertion has been done ? OR Is there any better approach to handle such type of scenario?
Essentially the third party API can not be made part of spring transaction. You can opt in one of following approaches. All these approaches have their pros and cons thus you'll have to choose depending upon what suits you best -