Commit d34dcb95 authored by duanruiming's avatar duanruiming

[update] 添加死锁重试机制

parent 37936b9f
package net.wanji.datacenter.service.impl; package net.wanji.datacenter.service.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.wanji.common.utils.tool.BeanListUtils; import net.wanji.common.utils.tool.BeanListUtils;
import net.wanji.databus.dao.mapper.*; import net.wanji.databus.dao.mapper.*;
...@@ -14,6 +15,7 @@ import net.wanji.datacenter.pojo.dto.CrossPeriodLaneDTO; ...@@ -14,6 +15,7 @@ import net.wanji.datacenter.pojo.dto.CrossPeriodLaneDTO;
import net.wanji.datacenter.pojo.dto.CrossPeriodTurnDTO; import net.wanji.datacenter.pojo.dto.CrossPeriodTurnDTO;
import net.wanji.datacenter.pojo.po.EventInfoPO; import net.wanji.datacenter.pojo.po.EventInfoPO;
import net.wanji.datacenter.service.DataProcessService; import net.wanji.datacenter.service.DataProcessService;
import org.apache.ignite.transactions.TransactionRollbackException;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -154,16 +156,31 @@ public class DataProcessServiceImpl implements DataProcessService { ...@@ -154,16 +156,31 @@ public class DataProcessServiceImpl implements DataProcessService {
} }
} }
private void insertTrafficIndexForEventInfo(List<CrossDataRealtimePO> eventList) { public void insertTrafficIndexForEventInfo(List<CrossDataRealtimePO> eventList) {
if (!CollectionUtils.isEmpty(eventList)) { if (!CollectionUtils.isEmpty(eventList)) {
for (CrossDataRealtimePO crossDataRealtimePO : eventList) { for (CrossDataRealtimePO crossDataRealtimePO : eventList) {
try { try {
int retryCount = 0;
String crossId = crossDataRealtimePO.getCrossId(); String crossId = crossDataRealtimePO.getCrossId();
EventInfoPO eventInfoPO = eventInfoMapper.selectLastEventInfo(crossId); EventInfoPO eventInfoPO = eventInfoMapper.selectLastEventInfo(crossId);
if (Objects.nonNull(eventInfoPO)) { if (Objects.nonNull(eventInfoPO)) {
Double trafficIndex = crossDataRealtimePO.getTrafficIndex(); Double trafficIndex = crossDataRealtimePO.getTrafficIndex();
eventInfoPO.setTrafficIndex(trafficIndex); eventInfoPO.setTrafficIndex(trafficIndex);
eventInfoMapper.updateOne(eventInfoPO); while (retryCount <= 3) {
try {
eventInfoMapper.updateOne(eventInfoPO);
return;
} catch (TransactionRollbackException e) {
retryCount++;
log.warn("检测到死锁,进行第 {} 次重试", retryCount);
try {
// 等待一会,避免立刻冲突
Thread.sleep(100);
} catch (InterruptedException ignored) {
log.warn("检测到死锁, Thread.sleep 100ms,异常:{}", ignored);
}
}
}
} }
} catch (Exception e) { } catch (Exception e) {
log.error("路口周期数据插入时间交通指数失败:{}", e); log.error("路口周期数据插入时间交通指数失败:{}", e);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment