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

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

@ -1,6 +1,7 @@
package cn.hippo4j.config.service.biz.impl; package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.enums.DelEnum;
import cn.hippo4j.common.toolkit.*; import cn.hippo4j.common.toolkit.*;
import cn.hippo4j.common.web.exception.ServiceException; import cn.hippo4j.common.web.exception.ServiceException;
import cn.hippo4j.config.event.LocalDataChangeEvent; import cn.hippo4j.config.event.LocalDataChangeEvent;
@ -113,16 +114,12 @@ public class ConfigServiceImpl implements ConfigService {
ConfigServiceImpl configService = ApplicationContextHolder.getBean(this.getClass()); ConfigServiceImpl configService = ApplicationContextHolder.getBean(this.getClass());
configInfo.setCapacity(getQueueCapacityByType(configInfo)); configInfo.setCapacity(getQueueCapacityByType(configInfo));
try { ConditionUtil
ConditionUtil .condition(
.condition( existConfig == null,
existConfig == null, () -> configService.addConfigInfo(configInfo),
() -> configService.addConfigInfo(configInfo), () -> configService.updateConfigInfo(identify, configInfo)
() -> configService.updateConfigInfo(identify, configInfo) );
);
} catch (Exception ex) {
updateConfigInfo(identify, configInfo);
}
ConfigChangePublisher.notifyConfigChange(new LocalDataChangeEvent(identify, ContentUtil.getGroupKey(configInfo))); ConfigChangePublisher.notifyConfigChange(new LocalDataChangeEvent(identify, ContentUtil.getGroupKey(configInfo)));
} }
@ -139,8 +136,18 @@ public class ConfigServiceImpl implements ConfigService {
config.setMd5(Md5Util.getTpContentMd5(config)); config.setMd5(Md5Util.getTpContentMd5(config));
try { try {
if (SqlHelper.retBool(configInfoMapper.insert(config))) { // 当前为单体应用, 后续支持集群部署时切换分布式锁.
return config.getId(); 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) { } catch (Exception ex) {
log.error("[db-error] message :: {}", ex.getMessage(), ex); log.error("[db-error] message :: {}", ex.getMessage(), ex);

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

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

Loading…
Cancel
Save