Commit f63920a6 authored by Matija Obreza's avatar Matija Obreza

updateAccessionCount called from outside transaction (causes too many locks)

parent 98cfb73b
......@@ -2,15 +2,18 @@ package org.genesys2.server.model.genesys;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SelfCopy {
private static Log LOG = LogFactory.getLog(SelfCopy.class);
private static Map<Method, Method> gettersSetters = new HashMap<Method, Method>();
public static void copy(AccessionData source, AccessionData target) {
Method[] allMethods = source.getClass().getMethods();
static {
Method[] allMethods = AccessionData.class.getMethods();
for (Method getter : allMethods) {
String name = getter.getName();
boolean isGet = false;
......@@ -23,33 +26,27 @@ public class SelfCopy {
name = name.substring(2);
}
LOG.debug("Getter coreName=" + name);
if (isGet) {
LOG.info("Using getter " + name);
LOG.info("Using getter " + getter);
try {
Object val = getter.invoke(source);
set(target, name, getter.getReturnType(), val);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
LOG.warn(e.getMessage(), e);
Method setter = AccessionData.class.getMethod("set" + name, getter.getReturnType());
gettersSetters.put(getter, setter);
LOG.info("Using setter " + setter);
} catch (NoSuchMethodException | SecurityException e) {
LOG.error("No setter for " + name);
}
}
}
}
private static void set(AccessionData target, String name, Class<?> type, Object val) throws NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
LOG.info("Using setter 'get" + name + "' to apply type " + type + " val=" + val);
try {
Method setter = target.getClass().getMethod("set" + name, type);
if (setter == null) {
LOG.error("No setter for " + name);
return;
public static void copy(AccessionData source, AccessionData target) {
for (Method getter : gettersSetters.keySet()) {
try {
Object val = getter.invoke(source);
gettersSetters.get(getter).invoke(target, val);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
LOG.error(e.getMessage(), e);
}
LOG.info("Setting property " + name);
setter.invoke(target, val);
} catch (NoSuchMethodException e) {
LOG.debug("No setter for " + name);
}
}
......
......@@ -689,7 +689,6 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
} else {
res = accessionRepository.save((Accession) accession);
}
updateAccessionCount(accession.getInstitute());
return res;
}
......@@ -735,8 +734,6 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
for (Accession a : toDelete) {
accessionRepository.deleteActive(a.getId());
}
updateAccessionCount(institute);
}
return deleted;
......
......@@ -16,6 +16,8 @@
package org.genesys2.server.service.impl;
import org.springframework.dao.CannotAcquireLockException;
public class RESTApiException extends Exception {
/**
......@@ -27,4 +29,8 @@ public class RESTApiException extends Exception {
super(message);
}
public RESTApiException(String message, Throwable cause) {
super(message, cause);
}
}
......@@ -50,6 +50,7 @@ import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
......@@ -224,10 +225,9 @@ 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);
for (int i = 0; i < 10; i++) {
try {
......@@ -292,9 +292,14 @@ public class AccessionController extends RestController {
throw new ResourceNotFoundException();
}
final int deleted = batchRESTService.deleteAccessions(institute, batch);
LOG.info("Deleted " + deleted + " accessions from " + instCode);
return new JsonDeleteResult(deleted);
try {
final int deleted = batchRESTService.deleteAccessions(institute, batch);
LOG.info("Deleted " + deleted + " accessions from " + instCode);
genesysService.updateAccessionCount(institute);
return new JsonDeleteResult(deleted);
} catch (CannotAcquireLockException e) {
throw new PleaseRetryException("Operation failed, please retry.", e);
}
}
/**
......@@ -317,6 +322,7 @@ public class AccessionController extends RestController {
final int deleted = batchRESTService.deleteAccessionsById(institute, batch);
LOG.info("Deleted " + deleted + " accessions from " + instCode);
genesysService.updateAccessionCount(institute);
return new JsonDeleteResult(deleted);
}
......
package org.genesys2.server.servlet.controller.rest;
import org.genesys2.server.service.impl.RESTApiException;
import org.springframework.dao.CannotAcquireLockException;
public class PleaseRetryException extends RESTApiException {
public PleaseRetryException(String message, Throwable cause) {
super(message, cause);
}
}
......@@ -25,7 +25,7 @@ db.url=jdbc:mysql://localhost/genesys?useUnicode=true&characterEncoding=UTF-8&us
db.driverClassName=com.mysql.jdbc.Driver
db.username=root
db.password=
db.showSql=true
db.showSql=false
db.hbm2ddl=false
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
......
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