diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/StaleSearchDeletingSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/StaleSearchDeletingSvcImpl.java index 1709c76e739..cbf41cf6c42 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/StaleSearchDeletingSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/StaleSearchDeletingSvcImpl.java @@ -37,6 +37,7 @@ import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionCallback; +import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; import java.util.Date; @@ -95,17 +96,24 @@ public class StaleSearchDeletingSvcImpl implements IStaleSearchDeletingSvc { ourLog.debug("Searching for searches which are before {}", cutoff); TransactionTemplate tt = new TransactionTemplate(myTransactionManager); - int count = tt.execute(new TransactionCallback() { + final Slice toDelete = tt.execute(new TransactionCallback>() { @Override - public Integer doInTransaction(TransactionStatus theStatus) { - Slice toDelete = mySearchDao.findWhereLastReturnedBefore(cutoff, new PageRequest(0, 1000)); - for (final Long next : toDelete) { - deleteSearch(next); - } - return toDelete.getContent().size(); + public Slice doInTransaction(TransactionStatus theStatus) { + return mySearchDao.findWhereLastReturnedBefore(cutoff, new PageRequest(0, 1000)); } }); + for (final Long nextSearchToDelete : toDelete) { + ourLog.debug("Deleting search with PID {}", nextSearchToDelete); + tt.execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus status) { + deleteSearch(nextSearchToDelete); + } + }); + } + + int count = toDelete.getContent().size(); if (count > 0) { long total = tt.execute(new TransactionCallback() { @Override diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d3c3b66303e..91a86772917 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -125,6 +125,12 @@ parameters that include additional content (e.g. _format=html/json;fhirVersion=1.0]]>) + + Stale search deleting routine on JPA server has been adjusted + to delete one search per transaction instead of batching 1000 + searches per transaction. This should make the deletion logic + more tolerant of deleting very large search result sets. +