我有一个石英作业,可以处理一个或多个选票。如果应添加投票的对象(CandidateResult)不存在,它将在添加投票之前创建一个新对象。我只是发现了一个错误,但我不明白为什么。
@Transactional(propagation = Propagation.REQUIRED)
public void processBallotResult(Context ctx,List<BallotResult> ballotResults,Election election) {
Map<String,CandidateResult> lookupMapCandidates = getResultCandidateLookupMap(election);
for (BallotResult ballotResult : ballotResults) {
Map<String,CandidateResult> candidatResult = candidateService.processCandidateResult(ctx,ballotResult,election);
if (!candidatResult.isEmpty()) {
internalElectionService.flush(ctx);
internalElectionService.refresh(ctx,election);
lookupMapCandidates = getResultCandidateLookupMap(election);
}
addBallotResultToCandidatesResult(election,lookupMapCandidates,ballotResult);
ballotResult.setStatus("PROCEDEED");
}
super.save(ctx,election);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Map<String,CandidateResult> processCandidateResult(Context ctx,BallotResult ballotResult,Election election) {
Map<String,CandidateResult> newCandidatesResults = new HashMap<>();
for(Result result : ballotResult.getResults()) {
Candidate candidate = result.getcandidate();
CandidateResult candidateResult = internalElectionService.createCandidateResult(candidate);
newCandidatesResults.put(candidate.getId(),candidateResult);
}
return newCandidatesResults;
}
为了使选举中具有新的CandidateResult,它将进行刷新和刷新。 问题在于,使用“ REQUIRES_NEW”后,在flush()和refresh()之后的迭代中,我不再有任何事务(在processCandidateResult函数中)。作业过程将无限期等待。
通过在processCandidateResult函数上将“ REQUIRES_NEW”替换为“ REQUIRED”,作业将获得新的交易记录并可以继续。
即使最后一个是使用flush()提交的,“ REQUIRES_NEW”是否也不应创建新事务?