Using Spring MVC, assume I have implemented a controller that handles a POST request, performs a database operation inside a transaction, and returns a result in the response body.
Here is the controller and service layer:
@RestController
@RequiredArgsConstructor
public class SomeController {
private final SomeService someService;
@PostMapping("/something")
public SomeResult postSomething(Something something) {
return someService.handle(something);
}
}
@Service
@RequiredArgsConstructor
public class SomeService {
private final SomeRepository someRepository;
@Transactional
public SomeResult handle(Something something){
// changes to the database
}
}
Questions:
Assuming someone pulls the network cable right after the service call, so the transaction is comitted.
1) Will Spring throw an exception if the response cannot be delivered?
2) Is it possible to rollback the transaction if the response cannot be delivered?
3) How can I make sure the database stays consistent when the client retries? (the POST is not idempotent).
Thanks!