Commit 0b600ddd authored by Matija Obreza's avatar Matija Obreza
Browse files

Catch HibernateOptimisticLockingFailureException and automatically retry upsert

parent 5627ea21
......@@ -53,6 +53,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -109,9 +110,8 @@ public class AccessionController extends RestController {
* @throws NonUniqueAccessionException
*/
@RequestMapping(value = "/exists/{instCode}/{genus:.+}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
boolean exists(@PathVariable("instCode") String instCode, @PathVariable("genus") String genus, @RequestParam("acceNumb") String acceNumb)
throws NonUniqueAccessionException {
public @ResponseBody boolean exists(@PathVariable("instCode") String instCode, @PathVariable("genus") String genus,
@RequestParam("acceNumb") String acceNumb) throws NonUniqueAccessionException {
if (LOG.isDebugEnabled()) {
LOG.debug("Checking if accn exists " + instCode + "." + acceNumb + " genus=" + genus);
}
......@@ -135,9 +135,8 @@ public class AccessionController extends RestController {
* @throws NonUniqueAccessionException
*/
@RequestMapping(value = "/{instCode}/check", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
JsonNode check(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException, RESTApiValueException,
NonUniqueAccessionException {
public @ResponseBody JsonNode check(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException,
RESTApiValueException, NonUniqueAccessionException {
final FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException("No institute " + instCode);
......@@ -197,9 +196,8 @@ public class AccessionController extends RestController {
* @throws RESTApiException
*/
@RequestMapping(value = "/{instCode}/upsert", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
String upsertInstituteAccession(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException,
RESTApiException {
public @ResponseBody String upsertInstituteAccession(@PathVariable("instCode") String instCode, @RequestBody String content)
throws JsonProcessingException, IOException, RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
final FaoInstitute institute = instituteService.getInstitute(instCode);
......@@ -226,8 +224,21 @@ public class AccessionController extends RestController {
batch.put(dataJson, (ObjectNode) json);
}
batchRESTService.upsertAccessionData(institute, batch);
return JSON_OK;
for (int i = 0; i < 10; i++) {
try {
batchRESTService.upsertAccessionData(institute, batch);
// Force update institute#accessionCount (outside previous
// transaction)
genesysService.updateAccessionCount(institute);
return JSON_OK;
} catch (HibernateOptimisticLockingFailureException e) {
LOG.info("Will retry upsert. Error: " + e.getMessage());
} catch (org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException e) {
LOG.info("Will retry upsert. Error: " + e.getMessage());
}
}
throw new RESTApiException("Could not upsert accession data.");
}
/**
......@@ -239,8 +250,8 @@ public class AccessionController extends RestController {
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/names", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
String upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch) throws RESTApiException {
public @ResponseBody String upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch)
throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
final FaoInstitute institute = instituteService.getInstitute(instCode);
......@@ -267,8 +278,8 @@ public class AccessionController extends RestController {
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete-named", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
JsonDeleteResult deleteAccessions(@PathVariable("instCode") String instCode, @RequestBody List<AccessionHeaderJson> batch) throws RESTApiException {
public @ResponseBody JsonDeleteResult deleteAccessions(@PathVariable("instCode") String instCode, @RequestBody List<AccessionHeaderJson> batch)
throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
final FaoInstitute institute = instituteService.getInstitute(instCode);
......@@ -290,8 +301,8 @@ public class AccessionController extends RestController {
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete", method = { RequestMethod.POST }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
JsonDeleteResult deleteAccessionsById(@PathVariable("instCode") String instCode, @RequestBody List<Long> batch) throws RESTApiException {
public @ResponseBody JsonDeleteResult deleteAccessionsById(@PathVariable("instCode") String instCode, @RequestBody List<Long> batch)
throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
final FaoInstitute institute = instituteService.getInstitute(instCode);
......@@ -305,16 +316,14 @@ public class AccessionController extends RestController {
}
@RequestMapping(value = "/search", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Page<AccessionDetails> search(@RequestParam("page") final int page, @RequestParam("query") final String query) throws SearchException {
public @ResponseBody Page<AccessionDetails> search(@RequestParam("page") final int page, @RequestParam("query") final String query) throws SearchException {
return elasticService.search(StringUtils.defaultIfBlank(query, "*"), new PageRequest(page - 1, 20));
}
// FIXME Not using institute...
@RequestMapping(value = "/{instCode}/list", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Page<AccessionDetails> list(@PathVariable("instCode") String instCode, @RequestParam("page") int page, @RequestParam("query") String query)
throws SearchException {
public @ResponseBody Page<AccessionDetails> list(@PathVariable("instCode") String instCode, @RequestParam("page") int page,
@RequestParam("query") String query) throws SearchException {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException();
......@@ -324,8 +333,7 @@ public class AccessionController extends RestController {
}
@RequestMapping(value = "/{id}", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
AccessionDetails getDetails(@PathVariable("id") long accessionId) {
public @ResponseBody AccessionDetails getDetails(@PathVariable("id") long accessionId) {
final AccessionDetails ad = genesysService.getAccessionDetails(accessionId);
if (ad == null) {
......@@ -336,8 +344,7 @@ public class AccessionController extends RestController {
}
@RequestMapping(value = "/list", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
List<AccessionJson> get(@RequestBody Set<Long> accessionIds) {
public @ResponseBody List<AccessionJson> get(@RequestBody Set<Long> accessionIds) {
return restService.getAccessionJSON(accessionIds);
}
......@@ -353,7 +360,7 @@ public class AccessionController extends RestController {
if (LOG.isDebugEnabled()) {
LOG.debug(appliedFilters.toString());
}
Page<Accession> accessions = filterService.listAccessions(appliedFilters, new PageRequest(jsonData.startAt - 1, jsonData.maxRecords, new Sort(
"acceNumb")));
return accessions;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment