Skip to content

Commit

Permalink
Fix #351: ActivationCleaningService needs transaction for stream proc…
Browse files Browse the repository at this point in the history
…essing (#410)
  • Loading branch information
banterCZ authored Oct 3, 2022
1 parent d515600 commit 0b7a063
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

/**
* Service to cleaning activations.
Expand All @@ -39,18 +42,23 @@ class ActivationCleaningService {

private final ActivationService activationService;

private final TransactionTemplate transactionTemplate;

@Autowired
public ActivationCleaningService(
final OnboardingProcessRepository onboardingProcessRepository,
final ActivationService activationService) {
final ActivationService activationService,
final TransactionTemplate transactionTemplate) {

this.onboardingProcessRepository = onboardingProcessRepository;
this.activationService = activationService;
this.transactionTemplate = transactionTemplate;
}

/**
* Cleanup activations of failed onboarding processes.
*/
@Transactional(readOnly = true)
public void cleanupActivations() {
onboardingProcessRepository.findProcessesToRemoveActivation()
.forEach(this::cleanupActivation);
Expand All @@ -63,7 +71,7 @@ private void cleanupActivation(final OnboardingProcessEntity process) {
try {
removeActivation(activationId);
process.setActivationRemoved(true);
onboardingProcessRepository.save(process);
saveInNewTransaction(process);
} catch (RemoteCommunicationException e) {
logger.error("Unable to remove activation ID: {}", activationId, e);
}
Expand All @@ -78,4 +86,9 @@ private void removeActivation(String activationId) throws RemoteCommunicationExc

activationService.removeActivation(activationId);
}

private void saveInNewTransaction(final OnboardingProcessEntity process) {
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
transactionTemplate.executeWithoutResult(status -> onboardingProcessRepository.save(process));
}
}

0 comments on commit 0b7a063

Please sign in to comment.