修复唯一主键插入冲突问题 #109

pull/117/head
chen.ma 2 years ago
parent 7ef05188df
commit 2587e71983

@ -1,6 +1,7 @@
package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.enums.DelEnum;
import cn.hippo4j.common.toolkit.*;
import cn.hippo4j.common.web.exception.ServiceException;
import cn.hippo4j.config.event.LocalDataChangeEvent;
@ -113,16 +114,12 @@ public class ConfigServiceImpl implements ConfigService {
ConfigServiceImpl configService = ApplicationContextHolder.getBean(this.getClass());
configInfo.setCapacity(getQueueCapacityByType(configInfo));
try {
ConditionUtil
.condition(
existConfig == null,
() -> configService.addConfigInfo(configInfo),
() -> configService.updateConfigInfo(identify, configInfo)
);
} catch (Exception ex) {
updateConfigInfo(identify, configInfo);
}
ConfigChangePublisher.notifyConfigChange(new LocalDataChangeEvent(identify, ContentUtil.getGroupKey(configInfo)));
}
@ -139,9 +136,19 @@ public class ConfigServiceImpl implements ConfigService {
config.setMd5(Md5Util.getTpContentMd5(config));
try {
// 当前为单体应用, 后续支持集群部署时切换分布式锁.
synchronized (ConfigService.class) {
ConfigAllInfo configAllInfo = configInfoMapper.selectOne(
Wrappers.lambdaQuery(ConfigAllInfo.class)
.eq(ConfigAllInfo::getTpId, config.getTpId())
.eq(ConfigAllInfo::getDelFlag, DelEnum.NORMAL.getIntCode())
);
Assert.isNull(configAllInfo, "线程池配置已存在.");
if (SqlHelper.retBool(configInfoMapper.insert(config))) {
return config.getId();
}
}
} catch (Exception ex) {
log.error("[db-error] message :: {}", ex.getMessage(), ex);
throw ex;

@ -77,8 +77,10 @@ public class ItemServiceImpl implements ItemService {
LambdaQueryWrapper<ItemInfo> queryWrapper = Wrappers.lambdaQuery(ItemInfo.class)
.eq(ItemInfo::getItemId, reqDTO.getItemId());
// 当前为单体应用, 后续支持集群部署时切换分布式锁.
synchronized (ItemService.class) {
ItemInfo existItemInfo = itemInfoMapper.selectOne(queryWrapper);
Assert.isNull(existItemInfo, "项目 ID 不允许重复.");
Assert.isNull(existItemInfo, "项目配置已存在.");
ItemInfo itemInfo = BeanUtil.convert(reqDTO, ItemInfo.class);
int insertResult = itemInfoMapper.insert(itemInfo);
@ -88,6 +90,7 @@ public class ItemServiceImpl implements ItemService {
throw new RuntimeException("Save error");
}
}
}
@Override
public void updateItem(ItemUpdateReqDTO reqDTO) {

@ -71,8 +71,10 @@ public class TenantServiceImpl implements TenantService {
LambdaQueryWrapper<TenantInfo> queryWrapper = Wrappers.lambdaQuery(TenantInfo.class)
.eq(TenantInfo::getTenantId, reqDTO.getTenantId());
// 当前为单体应用, 后续支持集群部署时切换分布式锁.
synchronized (TenantService.class) {
TenantInfo existTenantInfo = tenantInfoMapper.selectOne(queryWrapper);
Assert.isNull(existTenantInfo, "租户 ID 不允许重复.");
Assert.isNull(existTenantInfo, "租户配置已存在.");
TenantInfo tenantInfo = BeanUtil.convert(reqDTO, TenantInfo.class);
int insertResult = tenantInfoMapper.insert(tenantInfo);
@ -82,6 +84,7 @@ public class TenantServiceImpl implements TenantService {
throw new RuntimeException("Save Error.");
}
}
}
@Override
@LogRecord(

Loading…
Cancel
Save