Commit bcba3ad5 authored by Matija Obreza's avatar Matija Obreza

AccessionRef relinking after addAccessionRefs requires a delay

parent 8585cf19
...@@ -48,6 +48,13 @@ public interface DatasetAccessionRefRepository { ...@@ -48,6 +48,13 @@ public interface DatasetAccessionRefRepository {
* @param entities the existing DatasetAccessionRefs to update * @param entities the existing DatasetAccessionRefs to update
*/ */
void update(Iterable<DatasetAccessionRef> entities); 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. * Delete for dataset.
......
...@@ -71,6 +71,24 @@ public class DatasetAccessionRefRepositoryCustomImpl implements DatasetAccession ...@@ -71,6 +71,24 @@ public class DatasetAccessionRefRepositoryCustomImpl implements DatasetAccession
em.merge(entity); em.merge(entity);
} }
} }
@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) /* (non-Javadoc)
* @see org.genesys.catalog.persistence.dataset.DatasetAccessionRefRepository#save(java.lang.Iterable) * @see org.genesys.catalog.persistence.dataset.DatasetAccessionRefRepository#save(java.lang.Iterable)
......
...@@ -92,7 +92,9 @@ import org.springframework.transaction.TransactionStatus; ...@@ -92,7 +92,9 @@ import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; 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.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -617,22 +619,17 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -617,22 +619,17 @@ public class DatasetServiceImpl implements DatasetService {
datasetRepository.save(loadedDataset); datasetRepository.save(loadedDataset);
threadPoolTaskExecutor.execute(() -> { 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 { try {
// execute your business logic here Thread.sleep(500);
rematchDatasetAccessions(dArs); TransactionDefinition transactionDefinition = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);
} catch (Throwable ex) { new TransactionTemplate(transactionManager, transactionDefinition).execute(new TransactionCallbackWithoutResult() {
LOG.error("Rolling back rematch. Exception: {}", ex.getMessage(), ex); @Override
transactionManager.rollback(status); protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex; rematchDatasetAccessions(dArs);
}
});
} catch (InterruptedException e) {
} }
transactionManager.commit(status);
}); });
return lazyLoad(loadedDataset); return lazyLoad(loadedDataset);
} }
...@@ -1048,7 +1045,7 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -1048,7 +1045,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional(readOnly = false) @Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR')") @PreAuthorize("hasRole('ADMINISTRATOR')")
private List<DatasetAccessionRef> rematchDatasetAccessions(List<DatasetAccessionRef> accessionRefs) { 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); final AtomicInteger batchCounter = new AtomicInteger(0);
...@@ -1058,7 +1055,7 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -1058,7 +1055,7 @@ public class DatasetServiceImpl implements DatasetService {
lookupMatchingAccessions(batch); lookupMatchingAccessions(batch);
}); });
// Save in this transaction // Save in this transaction
accessionRefRepository.update(accessionRefs); accessionRefRepository.updateSafely(accessionRefs);
LOG.warn("Done relinking {} accession refs.", accessionRefs.size()); LOG.warn("Done relinking {} accession refs.", accessionRefs.size());
return accessionRefs; return accessionRefs;
} }
......
...@@ -50,6 +50,13 @@ public interface SubsetAccessionRefRepository { ...@@ -50,6 +50,13 @@ public interface SubsetAccessionRefRepository {
* @param entities the existing SubsetAccessionRefs to update * @param entities the existing SubsetAccessionRefs to update
*/ */
void update(Iterable<SubsetAccessionRef> entities); 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. * Delete for subset.
......
...@@ -75,6 +75,24 @@ public class SubsetAccessionRefRepositoryCustomImpl implements SubsetAccessionRe ...@@ -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) /* (non-Javadoc)
* @see org.genesys2.server.persistence.SubsetAccessionRefRepository#save(java.lang.Iterable) * @see org.genesys2.server.persistence.SubsetAccessionRefRepository#save(java.lang.Iterable)
*/ */
......
...@@ -73,7 +73,9 @@ import org.springframework.transaction.TransactionStatus; ...@@ -73,7 +73,9 @@ import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; 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.validation.annotation.Validated;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
...@@ -368,23 +370,19 @@ public class SubsetServiceImpl implements SubsetService { ...@@ -368,23 +370,19 @@ public class SubsetServiceImpl implements SubsetService {
LOG.info("Done saving {} accession refs, have {} in subset", accessionRefs.size(), loadedSubset.getAccessionCount()); LOG.info("Done saving {} accession refs, have {} in subset", accessionRefs.size(), loadedSubset.getAccessionCount());
subsetRepository.save(loadedSubset); subsetRepository.save(loadedSubset);
threadPoolTaskExecutor.execute(() -> { 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 { try {
// execute your business logic here Thread.sleep(500);
rematchSubsetAccessions(sArs); TransactionDefinition transactionDefinition = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
} catch (Throwable ex) { new TransactionTemplate(transactionManager, transactionDefinition).execute(new TransactionCallbackWithoutResult() {
LOG.error("Rolling back rematch. Exception: {}", ex.getMessage(), ex); @Override
transactionManager.rollback(status); protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex; rematchSubsetAccessions(sArs);
}
});
} catch (InterruptedException e) {
} }
transactionManager.commit(status);
}); });
return lazyLoad(loadedSubset); return lazyLoad(loadedSubset);
...@@ -623,7 +621,7 @@ public class SubsetServiceImpl implements SubsetService { ...@@ -623,7 +621,7 @@ public class SubsetServiceImpl implements SubsetService {
lookupMatchingAccessions(batch); lookupMatchingAccessions(batch);
}); });
// Save in this transaction // Save in this transaction
accessionRefRepository.update(accessionRefs); accessionRefRepository.updateSafely(accessionRefs);
LOG.info("Done relinking {} accession refs.", accessionRefs.size()); LOG.info("Done relinking {} accession refs.", accessionRefs.size());
return accessionRefs; 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