From 69e0b9642a397e2305f984d9c46b89a1bbc01112 Mon Sep 17 00:00:00 2001 From: luogw <3132758203@qq.com> Date: Tue, 24 Mar 2026 17:59:07 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/SaveClusterDomainServiceImpl.java | 15 ++++++++++++--- .../com/project/interaction/utils/NotifyUtil.java | 4 ---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/project/interaction/domain/service/impl/SaveClusterDomainServiceImpl.java b/src/main/java/com/project/interaction/domain/service/impl/SaveClusterDomainServiceImpl.java index 1783ceb..daab2d6 100644 --- a/src/main/java/com/project/interaction/domain/service/impl/SaveClusterDomainServiceImpl.java +++ b/src/main/java/com/project/interaction/domain/service/impl/SaveClusterDomainServiceImpl.java @@ -25,6 +25,9 @@ import com.project.task.domain.service.TaskBaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.support.TransactionSynchronization; +import org.springframework.transaction.support.TransactionSynchronizationManager; import java.util.*; import java.util.stream.Collectors; @@ -91,6 +94,7 @@ public class SaveClusterDomainServiceImpl implements SaveClusterDomainService { } @Override + @Transactional(rollbackFor = Exception.class) public void saveCluster(Long taskId, List clusters) { TaskEntity taskEntity = taskBaseService.getById(taskId); int u = 0; @@ -128,7 +132,7 @@ public class SaveClusterDomainServiceImpl implements SaveClusterDomainService { // 生成目标量 int targetNum = (int) Math.ceil(1.2 * u); //题目总数 - Integer totalNum = taskEntity.getMultipleChoiceNum() + taskEntity.getSingleChoiceNum() + taskEntity.getTrueFalseNum(); + int totalNum = taskEntity.getMultipleChoiceNum() + taskEntity.getSingleChoiceNum() + taskEntity.getTrueFalseNum(); //题目占比系数 Map questionTypeMap = new HashMap<>(); questionTypeMap.put(QuestionTypeEnum.SINGLE_CHOICE.name(), taskEntity.getSingleChoiceNum() * 1.0/totalNum); @@ -149,8 +153,13 @@ public class SaveClusterDomainServiceImpl implements SaveClusterDomainService { } } - //生成题目 - produce(produceDTOList); + // 事务提交后再执行题目生成(避免异步查询时数据未提交) + TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { + @Override + public void afterCommit() { + produce(produceDTOList); + } + }); } private void produce(List produceDTOList) { diff --git a/src/main/java/com/project/interaction/utils/NotifyUtil.java b/src/main/java/com/project/interaction/utils/NotifyUtil.java index 990a085..86014e5 100644 --- a/src/main/java/com/project/interaction/utils/NotifyUtil.java +++ b/src/main/java/com/project/interaction/utils/NotifyUtil.java @@ -39,10 +39,6 @@ public class NotifyUtil { public void notify(Long taskId,Long clusterId) { - if (taskId == null || clusterId == null) { - return; - } - // 构建 Redis 缓存 key(按 taskId 唯一) String cacheKey = WARN_LOCK_KEY_PREFIX + taskId;