Bläddra i källkod

解决风控管理中因事务问题导致查不到新建的登录log问题

zjy 1 månad sedan
förälder
incheckning
6254cf9e7e

+ 8 - 1
alien-gateway/src/main/java/shop/alien/gateway/service/LifeUserService.java

@@ -9,6 +9,7 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 import shop.alien.entity.second.LifeUserLog;
 import shop.alien.entity.second.SecondRiskControlRecord;
@@ -157,7 +158,8 @@ public class LifeUserService extends ServiceImpl<LifeUserGatewayMapper, LifeUser
             if (count > 0) {
                 String startDate = LocalDateTime.now().minusHours(24L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                 String endDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-                List<LifeUserLog> lsit = lifeUserLogMapper.getLifeUserLogByDate(startDate, endDate, macIp);
+                // 使用一个新的事务来查询,确保能看到刚才插入的数据
+                List<LifeUserLog> lsit = getLifeUserLogByDateInNewTransaction(startDate, endDate, macIp);
 
                 if (lsit.size() > riskControlProperties.getAccountAbnormal().getRegCount24h() && !isViolation(startDate, endDate, macIp, user.getId())) {
                     String detailInfo = lsit.stream()
@@ -174,6 +176,11 @@ public class LifeUserService extends ServiceImpl<LifeUserGatewayMapper, LifeUser
 
     }
 
+    @Transactional(propagation = Propagation.REQUIRES_NEW)
+    public List<LifeUserLog> getLifeUserLogByDateInNewTransaction(String startDate, String endDate, String macIp) {
+        return lifeUserLogMapper.getLifeUserLogByDate(startDate, endDate, macIp);
+    }
+
     public boolean isViolation(String startDate, String endDate, String macIp, Integer userId) {
         List<SecondRiskControlRecord> list = secondRiskControlRecordMapper.selectByBusinessId(startDate, endDate, macIp);
         for (SecondRiskControlRecord record : list) {