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

Retry upsert

parent 2c47208e
......@@ -50,6 +50,7 @@ import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.OrganizationService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.servlet.controller.rest.PleaseRetryException;
import org.genesys2.server.servlet.controller.rest.model.AccessionAliasJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
......@@ -760,7 +761,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (ensuredTaxonomy == null) {
LOG.warn("Could not find taxonomy for upsert " + current);
throw new RESTApiException("Could not find taxonomy " + current.getTaxonName());
throw new PleaseRetryException("Could not find taxonomy " + current.getTaxonName());
}
if (!ensuredTaxonomy.sameAs(taxonomy)) {
......
......@@ -137,8 +137,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);
......@@ -226,24 +226,27 @@ public class AccessionController extends RestController {
batch.put(dataJson, (ObjectNode) json);
}
// Step 1: Ensure all taxonomic data provided by client is persisted
batchRESTService.ensureTaxonomies(institute, batch);
Throwable cause = null;
for (int i = 0; i < 10; i++) {
try {
// Step 1: Ensure all taxonomic data provided by client is
// persisted
batchRESTService.ensureTaxonomies(institute, batch);
// Step 2: Upsert data
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) {
} catch (PleaseRetryException | HibernateOptimisticLockingFailureException
| org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException e) {
LOG.info("Will retry upsert. Error: " + e.getMessage());
cause = e;
}
}
throw new RESTApiException("Could not upsert accession data.");
throw new RESTApiException("Could not upsert accession data.", cause);
}
/**
......@@ -254,7 +257,8 @@ public class AccessionController extends RestController {
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/names", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
@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 {
// User's permission to WRITE to this WIEWS institute are checked in
......@@ -282,7 +286,8 @@ public class AccessionController extends RestController {
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete-named", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
@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 {
// User's permission to WRITE to this WIEWS institute are checked in
......@@ -310,7 +315,8 @@ public class AccessionController extends RestController {
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete", method = { RequestMethod.POST }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
@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 {
// User's permission to WRITE to this WIEWS institute are checked in
......@@ -372,8 +378,8 @@ public class AccessionController extends RestController {
LOG.debug(appliedFilters.toString());
}
Page<Accession> accessions = filterService.listAccessions(appliedFilters, new PageRequest(jsonData.startAt - 1, jsonData.maxRecords, new Sort(
"acceNumb")));
Page<Accession> accessions = filterService.listAccessions(appliedFilters,
new PageRequest(jsonData.startAt - 1, jsonData.maxRecords, new Sort("acceNumb")));
return accessions;
}
......
......@@ -9,8 +9,11 @@ public class PleaseRetryException extends RESTApiException {
*/
private static final long serialVersionUID = 456286423902987649L;
public PleaseRetryException(String message) {
super(message);
}
public PleaseRetryException(String message, Throwable cause) {
super(message, cause);
}
}
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