Commit bcba3ad5 authored by Matija Obreza's avatar Matija Obreza

AccessionRef relinking after addAccessionRefs requires a delay

parent 8585cf19
......@@ -49,6 +49,13 @@ public interface DatasetAccessionRefRepository {
*/
void update(Iterable<DatasetAccessionRef> entities);
/**
* Update safely: check if records exist before persisting.
*
* @param entities the entities
*/
void updateSafely(Iterable<DatasetAccessionRef> entities);
/**
* Delete for dataset.
*
......
......@@ -72,6 +72,24 @@ public class DatasetAccessionRefRepositoryCustomImpl implements DatasetAccession
}
}
@Override
@Transactional
public void updateSafely(Iterable<DatasetAccessionRef> entities) {
for (DatasetAccessionRef entity: entities) {
if (exists(entity)) {
em.merge(entity);
} else {
LOG.warn("Record is gone {}", entity);
}
}
}
private boolean exists(DatasetAccessionRef ref) {
return jpaQueryFactory.selectFrom(QDatasetAccessionRef.datasetAccessionRef).where(QDatasetAccessionRef.datasetAccessionRef.dataset.eq(ref.getDataset()).and(
QDatasetAccessionRef.datasetAccessionRef.instCode.eq(ref.getInstCode()).and(QDatasetAccessionRef.datasetAccessionRef.genus.eq(ref.getGenus()).and(
QDatasetAccessionRef.datasetAccessionRef.acceNumb.eq(ref.getAcceNumb()))))).fetchCount() == 1;
}
/* (non-Javadoc)
* @see org.genesys.catalog.persistence.dataset.DatasetAccessionRefRepository#save(java.lang.Iterable)
*/
......
......@@ -92,7 +92,9 @@ import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
......@@ -617,22 +619,17 @@ public class DatasetServiceImpl implements DatasetService {
datasetRepository.save(loadedDataset);
threadPoolTaskExecutor.execute(() -> {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
// explicitly setting the transaction name is something that can only be done
// programmatically
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = transactionManager.getTransaction(def);
try {
// execute your business logic here
Thread.sleep(500);
TransactionDefinition transactionDefinition = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);
new TransactionTemplate(transactionManager, transactionDefinition).execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
rematchDatasetAccessions(dArs);
} catch (Throwable ex) {
LOG.error("Rolling back rematch. Exception: {}", ex.getMessage(), ex);
transactionManager.rollback(status);
throw ex;
}
transactionManager.commit(status);
});
} catch (InterruptedException e) {
}
});
return lazyLoad(loadedDataset);
}
......@@ -1048,7 +1045,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR')")
private List<DatasetAccessionRef> rematchDatasetAccessions(List<DatasetAccessionRef> accessionRefs) {
LOG.warn("Re-linking {} accessions refs with accessions", accessionRefs.size());
LOG.debug("Re-linking {} accessions refs with accessions", accessionRefs.size());
final AtomicInteger batchCounter = new AtomicInteger(0);
......@@ -1058,7 +1055,7 @@ public class DatasetServiceImpl implements DatasetService {
lookupMatchingAccessions(batch);
});
// Save in this transaction
accessionRefRepository.update(accessionRefs);
accessionRefRepository.updateSafely(accessionRefs);
LOG.warn("Done relinking {} accession refs.", accessionRefs.size());
return accessionRefs;
}
......
......@@ -51,6 +51,13 @@ public interface SubsetAccessionRefRepository {
*/
void update(Iterable<SubsetAccessionRef> entities);
/**
* Update safely: check if records exist before persisting.
*
* @param entities the entities
*/
void updateSafely(Iterable<SubsetAccessionRef> entities);
/**
* Delete for subset.
*
......
......@@ -75,6 +75,24 @@ public class SubsetAccessionRefRepositoryCustomImpl implements SubsetAccessionRe
}
}
@Override
@Transactional
public void updateSafely(Iterable<SubsetAccessionRef> entities) {
for (SubsetAccessionRef entity: entities) {
if (exists(entity)) {
em.merge(entity);
} else {
LOG.warn("Record is gone {}", entity);
}
}
}
private boolean exists(SubsetAccessionRef ref) {
return jpaQueryFactory.selectFrom(QSubsetAccessionRef.subsetAccessionRef).where(QSubsetAccessionRef.subsetAccessionRef.subset.eq(ref.getSubset()).and(
QSubsetAccessionRef.subsetAccessionRef.instCode.eq(ref.getInstCode()).and(QSubsetAccessionRef.subsetAccessionRef.genus.eq(ref.getGenus()).and(
QSubsetAccessionRef.subsetAccessionRef.acceNumb.eq(ref.getAcceNumb()))))).fetchCount() == 1;
}
/* (non-Javadoc)
* @see org.genesys2.server.persistence.SubsetAccessionRefRepository#save(java.lang.Iterable)
*/
......
......@@ -73,7 +73,9 @@ import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.validation.annotation.Validated;
import com.google.common.collect.Iterables;
......@@ -368,23 +370,19 @@ public class SubsetServiceImpl implements SubsetService {
LOG.info("Done saving {} accession refs, have {} in subset", accessionRefs.size(), loadedSubset.getAccessionCount());
subsetRepository.save(loadedSubset);
threadPoolTaskExecutor.execute(() -> {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
// explicitly setting the transaction name is something that can only be done
// programmatically
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = transactionManager.getTransaction(def);
threadPoolTaskExecutor.execute(() -> {
try {
// execute your business logic here
Thread.sleep(500);
TransactionDefinition transactionDefinition = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
new TransactionTemplate(transactionManager, transactionDefinition).execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
rematchSubsetAccessions(sArs);
} catch (Throwable ex) {
LOG.error("Rolling back rematch. Exception: {}", ex.getMessage(), ex);
transactionManager.rollback(status);
throw ex;
}
transactionManager.commit(status);
});
} catch (InterruptedException e) {
}
});
return lazyLoad(loadedSubset);
......@@ -623,7 +621,7 @@ public class SubsetServiceImpl implements SubsetService {
lookupMatchingAccessions(batch);
});
// Save in this transaction
accessionRefRepository.update(accessionRefs);
accessionRefRepository.updateSafely(accessionRefs);
LOG.info("Done relinking {} accession refs.", accessionRefs.size());
return accessionRefs;
}
......
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