My website has a form with multiple sections which will call /save API every time user click next page / next section, so the API is often triggered. And when the API is triggered frequently within a short period it will hit LockAcquisitionException. Below is my code:
@ApiOperation(nickname = "updateForm", value = "update/save form data for each section. Recommended to update at the end of each section.")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ResponseEntity<Void> saveForm(@RequestBody @Valid Form form) {
RecordBean recordBean = recordWebService.saveRecord(acctId, form, null);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@Transactional
@Override
public RecordBean saveRecord(Integer adviserId, Form form, List<Attachment> attachments) {
String id = form.getId();
RecordBean record = recordService.getRecord(id, adviserId, form.getKey());
Assert.notNull(record, "record not found");
(some read and write actions here)
...
recordService.updateRecord(record);
return record;
}
I cannot change the logic of the API due to legacy code, how can I avoid the deadlock?