For #10353, Handling DataIntegrityViolationException and DuplicateKeyException together. (#11405)

This commit is contained in:
杨翊 SionYang 2023-11-20 15:04:34 +08:00 committed by GitHub
parent 03987330a4
commit 6a4d740781
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -132,8 +132,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
this.dataSourceService = DynamicDataSource.getInstance().getDataSource();
this.jt = dataSourceService.getJdbcTemplate();
this.tjt = dataSourceService.getTransactionTemplate();
Boolean isDataSourceLogEnable = EnvUtil.getProperty(CommonConstant.NACOS_PLUGIN_DATASOURCE_LOG, Boolean.class,
false);
Boolean isDataSourceLogEnable = EnvUtil
.getProperty(CommonConstant.NACOS_PLUGIN_DATASOURCE_LOG, Boolean.class, false);
this.mapperManager = MapperManager.instance(isDataSourceLogEnable);
this.historyConfigInfoPersistService = historyConfigInfoPersistService;
}
@ -166,8 +166,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
Timestamp now = new Timestamp(System.currentTimeMillis());
historyConfigInfoPersistService.insertConfigHistoryAtomic(0, configInfo, srcIp, srcUser, now, "I");
ConfigInfoStateWrapper configInfoCurrent = this.findConfigInfoState(configInfo.getDataId(),
configInfo.getGroup(), configInfo.getTenant());
ConfigInfoStateWrapper configInfoCurrent = this
.findConfigInfoState(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
if (configInfoCurrent == null) {
return new ConfigOperateResult(false);
}
@ -193,8 +193,19 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
Map<String, Object> configAdvanceInfo) {
try {
return addConfigInfo(srcIp, srcUser, configInfo, configAdvanceInfo);
} catch (DuplicateKeyException ive) { // Unique constraint conflict
} catch (DuplicateKeyException ignore) {
// Must be unique constraint conflict.
return updateConfigInfo(configInfo, srcIp, srcUser, configAdvanceInfo);
} catch (DataIntegrityViolationException dive) {
// Might be unique constraint conflict or some of other data integrity violation. try to update.
try {
return updateConfigInfo(configInfo, srcIp, srcUser, configAdvanceInfo);
} catch (Exception e) {
LogUtil.FATAL_LOG
.error("[db-error] try to update config failed, with DataIntegrityViolationException, {}",
dive.getMessage(), e);
throw e;
}
}
}
@ -203,8 +214,19 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
Map<String, Object> configAdvanceInfo) {
try {
return addConfigInfo(srcIp, srcUser, configInfo, configAdvanceInfo);
} catch (DuplicateKeyException ignore) { // Unique constraint conflict
} catch (DuplicateKeyException ignore) {
// Must be unique constraint conflict.
return updateConfigInfoCas(configInfo, srcIp, srcUser, configAdvanceInfo);
} catch (DataIntegrityViolationException dive) {
// Might be unique constraint conflict or some of other data integrity violation. try to update.
try {
return updateConfigInfoCas(configInfo, srcIp, srcUser, configAdvanceInfo);
} catch (Exception e) {
LogUtil.FATAL_LOG
.error("[db-error] try to update config cas failed, with DataIntegrityViolationException, {}",
dive.getMessage(), e);
throw e;
}
}
}
@ -226,10 +248,10 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
KeyHolder keyHolder = new GeneratedKeyHolder();
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
final String sql = configInfoMapper.insert(
Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", "src_user",
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
final String sql = configInfoMapper.insert(Arrays
.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", "src_user",
"gmt_create", "gmt_modified", "c_desc", "c_use", "effect", "type", "c_schema",
"encrypted_data_key"));
String[] returnGeneratedKeys = configInfoMapper.getPrimaryKeyGeneratedKeys();
@ -273,11 +295,11 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public void addConfigTagRelationAtomic(long configId, String tagName, String dataId, String group, String tenant) {
try {
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager.findMapper(
dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
jt.update(configTagsRelationMapper.insert(
Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id")), configId, tagName,
StringUtils.EMPTY, dataId, group, tenant);
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
jt.update(configTagsRelationMapper
.insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id")), configId,
tagName, StringUtils.EMPTY, dataId, group, tenant);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
throw e;
@ -305,8 +327,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
for (int i = 0; i < configInfoList.size(); i++) {
ConfigAllInfo configInfo = configInfoList.get(i);
try {
ParamUtils.checkParam(configInfo.getDataId(), configInfo.getGroup(), "datumId",
configInfo.getContent());
ParamUtils
.checkParam(configInfo.getDataId(), configInfo.getGroup(), "datumId", configInfo.getContent());
} catch (NacosException e) {
LogUtil.DEFAULT_LOG.error("data verification failed", e);
throw e;
@ -392,8 +414,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
if (configInfo != null) {
removeConfigInfoAtomic(dataId, group, tenant, srcIp, srcUser);
removeTagByIdAtomic(configInfo.getId());
historyConfigInfoPersistService.insertConfigHistoryAtomic(configInfo.getId(), configInfo, srcIp,
srcUser, time, "D");
historyConfigInfoPersistService
.insertConfigHistoryAtomic(configInfo.getId(), configInfo, srcIp, srcUser, time, "D");
}
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
@ -422,8 +444,9 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
removeConfigInfoByIdsAtomic(idsStr);
for (ConfigInfo configInfo : configInfoList) {
removeTagByIdAtomic(configInfo.getId());
historyConfigInfoPersistService.insertConfigHistoryAtomic(configInfo.getId(), configInfo,
srcIp, srcUser, time, "D");
historyConfigInfoPersistService
.insertConfigHistoryAtomic(configInfo.getId(), configInfo, srcIp, srcUser, time,
"D");
}
}
return configInfoList;
@ -438,8 +461,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public void removeTagByIdAtomic(long id) {
try {
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager.findMapper(
dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
jt.update(configTagsRelationMapper.delete(Collections.singletonList("id")), id);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
@ -452,8 +475,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
final String srcUser) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
jt.update(configInfoMapper.delete(Arrays.asList("data_id", "group_id", "tenant_id")), dataId, group,
tenantTmp);
} catch (CannotGetJdbcConnectionException e) {
@ -472,8 +495,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
for (int i = 0; i < idArr.length; i++) {
paramList.add(Long.parseLong(idArr[i]));
}
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext();
context.putWhereParameter(FieldConstant.IDS, paramList);
MapperResult result = configInfoMapper.removeConfigInfoByIdsAtomic(context);
@ -518,8 +541,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
}
Timestamp now = new Timestamp(System.currentTimeMillis());
historyConfigInfoPersistService.insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp,
srcUser, now, "U");
historyConfigInfoPersistService
.insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp, srcUser, now, "U");
return getConfigInfoOperateResult(configInfo.getDataId(), configInfo.getGroup(),
configInfo.getTenant());
} catch (CannotGetJdbcConnectionException e) {
@ -573,10 +596,10 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
}
Timestamp now = new Timestamp(System.currentTimeMillis());
historyConfigInfoPersistService.insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp,
srcUser, now, "U");
ConfigInfoStateWrapper configInfoLast = this.findConfigInfoState(configInfo.getDataId(),
configInfo.getGroup(), configInfo.getTenant());
historyConfigInfoPersistService
.insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp, srcUser, now, "U");
ConfigInfoStateWrapper configInfoLast = this
.findConfigInfoState(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
if (configInfoLast == null) {
return new ConfigOperateResult(false);
}
@ -600,8 +623,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
String schema = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("schema");
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
Timestamp now = new Timestamp(System.currentTimeMillis());
MapperContext context = new MapperContext();
@ -645,14 +668,14 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
configInfo.getEncryptedDataKey() == null ? StringUtils.EMPTY : configInfo.getEncryptedDataKey();
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
Timestamp now = new Timestamp(System.currentTimeMillis());
jt.update(configInfoMapper.update(
Arrays.asList("content", "md5", "src_ip", "src_user", "gmt_modified", "app_name", "c_desc", "c_use",
jt.update(configInfoMapper.update(Arrays
.asList("content", "md5", "src_ip", "src_user", "gmt_modified", "app_name", "c_desc", "c_use",
"effect", "type", "c_schema", "encrypted_data_key"),
Arrays.asList("data_id", "group_id", "tenant_id")), configInfo.getContent(), md5Tmp, srcIp, srcUser,
Arrays.asList("data_id", "group_id", "tenant_id")), configInfo.getContent(), md5Tmp, srcIp, srcUser,
now, appNameTmp, desc, use, effect, type, schema, encryptedDataKey, configInfo.getDataId(),
configInfo.getGroup(), tenantTmp);
} catch (CannotGetJdbcConnectionException e) {
@ -665,10 +688,10 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
public void updateMd5(String dataId, String group, String tenant, String md5, Timestamp lastTime) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
jt.update(configInfoMapper.update(Collections.singletonList("md5"),
Arrays.asList("data_id", "group_id", "tenant_id", "gmt_modified")), md5, dataId, group, tenantTmp,
Arrays.asList("data_id", "group_id", "tenant_id", "gmt_modified")), md5, dataId, group, tenantTmp,
lastTime);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
@ -678,8 +701,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public long findConfigMaxId() {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperResult mapperResult = configInfoMapper.findConfigMaxId(null);
try {
@ -692,8 +715,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Deprecated
@Override
public List<ConfigInfo> findAllDataIdAndGroup() {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperResult mapperResult = configInfoMapper.findAllDataIdAndGroup(null);
try {
@ -712,10 +735,10 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public ConfigInfoBase findConfigInfoBase(final String dataId, final String group) {
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
return this.jt.queryForObject(configInfoMapper.select(Arrays.asList("id", "data_id", "group_id", "content"),
Arrays.asList("data_id", "group_id", "tenant_id")), new Object[] {dataId, group, StringUtils.EMPTY},
Arrays.asList("data_id", "group_id", "tenant_id")), new Object[] {dataId, group, StringUtils.EMPTY},
CONFIG_INFO_BASE_ROW_MAPPER);
} catch (EmptyResultDataAccessException e) { // Indicates that the data does not exist, returns null.
return null;
@ -728,11 +751,11 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public ConfigInfo findConfigInfo(long id) {
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
return this.jt.queryForObject(configInfoMapper.select(
Arrays.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content"),
Collections.singletonList("id")), new Object[] {id}, CONFIG_INFO_ROW_MAPPER);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
return this.jt.queryForObject(configInfoMapper
.select(Arrays.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content"),
Collections.singletonList("id")), new Object[] {id}, CONFIG_INFO_ROW_MAPPER);
} catch (EmptyResultDataAccessException e) { // Indicates that the data does not exist, returns null.
return null;
} catch (CannotGetJdbcConnectionException e) {
@ -745,10 +768,10 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
public ConfigInfoWrapper findConfigInfo(final String dataId, final String group, final String tenant) {
final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
return this.jt.queryForObject(configInfoMapper.select(
Arrays.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "md5", "type",
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
return this.jt.queryForObject(configInfoMapper.select(Arrays
.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "md5", "type",
"encrypted_data_key", "gmt_modified"), Arrays.asList("data_id", "group_id", "tenant_id")),
new Object[] {dataId, group, tenantTmp}, CONFIG_INFO_WRAPPER_ROW_MAPPER);
} catch (EmptyResultDataAccessException e) { // Indicates that the data does not exist, returns null.
@ -791,13 +814,13 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
if (StringUtils.isNotBlank(configTags)) {
String[] tagArr = configTags.split(",");
context.putWhereParameter(FieldConstant.TAG_ARR, Arrays.asList(tagArr));
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager.findMapper(
dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
sqlCount = configTagsRelationMapper.findConfigInfo4PageCountRows(context);
sql = configTagsRelationMapper.findConfigInfo4PageFetchRows(context);
} else {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
sqlCount = configInfoMapper.findConfigInfo4PageCountRows(context);
sql = configInfoMapper.findConfigInfo4PageFetchRows(context);
@ -805,8 +828,9 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
try {
Page<ConfigInfo> page = helper.fetchPageLimit(sqlCount, sql, pageNo, pageSize, CONFIG_INFO_ROW_MAPPER);
for (ConfigInfo configInfo : page.getPageItems()) {
Pair<String, String> pair = EncryptionHandler.decryptHandler(configInfo.getDataId(),
configInfo.getEncryptedDataKey(), configInfo.getContent());
Pair<String, String> pair = EncryptionHandler
.decryptHandler(configInfo.getDataId(), configInfo.getEncryptedDataKey(),
configInfo.getContent());
configInfo.setContent(pair.getSecond());
}
return page;
@ -823,8 +847,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
PaginationHelper<ConfigInfo> helper = createPaginationHelper();
try {
final int startRow = (pageNo - 1) * pageSize;
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext(startRow, pageSize);
context.putWhereParameter(FieldConstant.APP_NAME, appName);
context.putWhereParameter(FieldConstant.TENANT_ID, generateLikeArgument(tenantTmp));
@ -844,17 +868,17 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
PaginationHelper<ConfigInfoBase> helper = createPaginationHelper();
try {
final int startRow = (pageNo - 1) * pageSize;
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext(startRow, pageSize);
context.putWhereParameter(FieldConstant.GROUP_ID, group);
context.putWhereParameter(FieldConstant.TENANT_ID, StringUtils.EMPTY);
MapperResult mapperResult = configInfoMapper.findConfigInfoBaseByGroupFetchRows(context);
return helper.fetchPage(configInfoMapper.count(Arrays.asList("group_id", "tenant_id")),
mapperResult.getSql(), mapperResult.getParamList().toArray(), pageNo, pageSize,
CONFIG_INFO_BASE_ROW_MAPPER);
return helper
.fetchPage(configInfoMapper.count(Arrays.asList("group_id", "tenant_id")), mapperResult.getSql(),
mapperResult.getParamList().toArray(), pageNo, pageSize, CONFIG_INFO_BASE_ROW_MAPPER);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
throw e;
@ -863,8 +887,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public int configInfoCount() {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
String sql = configInfoMapper.count(null);
Integer result = jt.queryForObject(sql, Integer.class);
if (result == null) {
@ -875,8 +899,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public int configInfoCount(String tenant) {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext();
context.putWhereParameter(FieldConstant.TENANT_ID, tenant);
MapperResult mapperResult = configInfoMapper.configInfoLikeTenantCount(context);
@ -889,8 +913,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public List<String> getTenantIdList(int page, int pageSize) {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
int from = (page - 1) * pageSize;
MapperResult mapperResult = configInfoMapper.getTenantIdList(new MapperContext(from, pageSize));
return jt.queryForList(mapperResult.getSql(), mapperResult.getParamList().toArray(), String.class);
@ -898,8 +922,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public List<String> getGroupIdList(int page, int pageSize) {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
int from = (page - 1) * pageSize;
MapperResult mapperResult = configInfoMapper.getGroupIdList(new MapperContext(from, pageSize));
return jt.queryForList(mapperResult.getSql(), mapperResult.getParamList().toArray(), String.class);
@ -909,16 +933,17 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
public Page<ConfigInfo> findAllConfigInfo(final int pageNo, final int pageSize, final String tenant) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
final int startRow = (pageNo - 1) * pageSize;
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
String sqlCountRows = configInfoMapper.count(null);
MapperContext context = new MapperContext(startRow, pageSize);
context.putWhereParameter(FieldConstant.TENANT_ID, generateLikeArgument(generateLikeArgument(tenantTmp)));
MapperResult sqlFetchRows = configInfoMapper.findAllConfigInfoFetchRows(context);
PaginationHelper<ConfigInfo> helper = createPaginationHelper();
try {
return helper.fetchPageLimit(sqlCountRows, sqlFetchRows.getSql(), sqlFetchRows.getParamList().toArray(),
pageNo, pageSize, CONFIG_INFO_ROW_MAPPER);
return helper
.fetchPageLimit(sqlCountRows, sqlFetchRows.getSql(), sqlFetchRows.getParamList().toArray(), pageNo,
pageSize, CONFIG_INFO_ROW_MAPPER);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
throw e;
@ -928,8 +953,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public Page<ConfigKey> findAllConfigKey(final int pageNo, final int pageSize, final String tenant) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
int startRow = (pageNo - 1) * pageSize;
MapperContext context = new MapperContext(startRow, pageSize);
context.putWhereParameter(FieldConstant.TENANT_ID, generateLikeArgument(tenantTmp));
@ -951,8 +976,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
page.setTotalCount(totalCount);
try {
List<ConfigKey> result = jt.query(mapperResult.getSql(), mapperResult.getParamList().toArray(),
CONFIG_KEY_ROW_MAPPER);
List<ConfigKey> result = jt
.query(mapperResult.getSql(), mapperResult.getParamList().toArray(), CONFIG_KEY_ROW_MAPPER);
for (ConfigKey item : result) {
page.getPageItems().add(item);
@ -968,8 +993,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public Page<ConfigInfoWrapper> findAllConfigInfoFragment(final long lastMaxId, final int pageSize) {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext(0, pageSize);
context.putWhereParameter(FieldConstant.ID, lastMaxId);
MapperResult select = configInfoMapper.findAllConfigInfoFragment(context);
@ -1112,24 +1137,25 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
if (StringUtils.isNotBlank(configTags)) {
String[] tagArr = configTags.split(",");
context.putWhereParameter(FieldConstant.TAG_ARR, tagArr);
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager.findMapper(
dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
sqlCountRows = configTagsRelationMapper.findConfigInfoLike4PageCountRows(context);
sqlFetchRows = configTagsRelationMapper.findConfigInfoLike4PageFetchRows(context);
} else {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
sqlCountRows = configInfoMapper.findConfigInfoLike4PageCountRows(context);
sqlFetchRows = configInfoMapper.findConfigInfoLike4PageFetchRows(context);
}
try {
Page<ConfigInfo> page = helper.fetchPageLimit(sqlCountRows, sqlFetchRows, pageNo, pageSize,
CONFIG_INFO_ROW_MAPPER);
Page<ConfigInfo> page = helper
.fetchPageLimit(sqlCountRows, sqlFetchRows, pageNo, pageSize, CONFIG_INFO_ROW_MAPPER);
for (ConfigInfo configInfo : page.getPageItems()) {
Pair<String, String> pair = EncryptionHandler.decryptHandler(configInfo.getDataId(),
configInfo.getEncryptedDataKey(), configInfo.getContent());
Pair<String, String> pair = EncryptionHandler
.decryptHandler(configInfo.getDataId(), configInfo.getEncryptedDataKey(),
configInfo.getContent());
configInfo.setContent(pair.getSecond());
}
return page;
@ -1157,8 +1183,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
context.putWhereParameter(FieldConstant.CONTENT, generateLikeArgument(content));
}
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperResult sqlCountRows = configInfoMapper.findConfigInfoBaseLikeCountRows(context);
MapperResult sqlFetchRows = configInfoMapper.findConfigInfoBaseLikeFetchRows(context);
PaginationHelper<ConfigInfoBase> helper = createPaginationHelper();
@ -1173,17 +1199,17 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public List<ConfigInfoWrapper> findChangeConfig(final Timestamp startTime, long lastMaxId, final int pageSize) {
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext();
context.putWhereParameter(FieldConstant.START_TIME, startTime);
context.putWhereParameter(FieldConstant.PAGE_SIZE, pageSize);
context.putWhereParameter(FieldConstant.LAST_MAX_ID, lastMaxId);
MapperResult mapperResult = configInfoMapper.findChangeConfig(context);
List<Map<String, Object>> list = jt.queryForList(mapperResult.getSql(),
mapperResult.getParamList().toArray());
List<Map<String, Object>> list = jt
.queryForList(mapperResult.getSql(), mapperResult.getParamList().toArray());
return convertChangeConfig(list);
} catch (DataAccessException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
@ -1219,8 +1245,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
context.putWhereParameter(FieldConstant.END_TIME, endTime);
}
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperResult sqlCountRows = configInfoMapper.findChangeConfigCountRows(context);
MapperResult sqlFetchRows = configInfoMapper.findChangeConfigFetchRows(context);
@ -1236,10 +1262,10 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public List<String> selectTagByConfig(String dataId, String group, String tenant) {
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager.findMapper(
dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
String sql = configTagsRelationMapper.select(Collections.singletonList("tag_name"),
Arrays.asList("data_id", "group_id", "tenant_id"));
ConfigTagsRelationMapper configTagsRelationMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION);
String sql = configTagsRelationMapper
.select(Collections.singletonList("tag_name"), Arrays.asList("data_id", "group_id", "tenant_id"));
try {
return jt.queryForList(sql, new Object[] {dataId, group, tenant}, String.class);
} catch (IncorrectResultSizeDataAccessException e) {
@ -1260,8 +1286,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
for (int i = 0; i < idArr.length; i++) {
paramList.add(Long.parseLong(idArr[i]));
}
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext();
context.putWhereParameter(FieldConstant.IDS, paramList);
MapperResult mapperResult = configInfoMapper.findConfigInfosByIds(context);
@ -1281,11 +1307,11 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
try {
List<String> configTagList = this.selectTagByConfig(dataId, group, tenant);
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigAdvanceInfo configAdvance = this.jt.queryForObject(configInfoMapper.select(
Arrays.asList("gmt_create", "gmt_modified", "src_user", "src_ip", "c_desc", "c_use", "effect",
"type", "c_schema"), Arrays.asList("data_id", "group_id", "tenant_id")),
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
ConfigAdvanceInfo configAdvance = this.jt.queryForObject(configInfoMapper.select(Arrays
.asList("gmt_create", "gmt_modified", "src_user", "src_ip", "c_desc", "c_use", "effect", "type",
"c_schema"), Arrays.asList("data_id", "group_id", "tenant_id")),
new Object[] {dataId, group, tenantTmp}, CONFIG_ADVANCE_INFO_ROW_MAPPER);
if (configTagList != null && !configTagList.isEmpty()) {
StringBuilder configTagsTmp = new StringBuilder();
@ -1312,10 +1338,10 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
try {
List<String> configTagList = this.selectTagByConfig(dataId, group, tenant);
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigAllInfo configAdvance = this.jt.queryForObject(configInfoMapper.select(
Arrays.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "md5", "gmt_create",
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
ConfigAllInfo configAdvance = this.jt.queryForObject(configInfoMapper.select(Arrays
.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "md5", "gmt_create",
"gmt_modified", "src_user", "src_ip", "c_desc", "c_use", "effect", "type", "c_schema",
"encrypted_data_key"), Arrays.asList("data_id", "group_id", "tenant_id")),
new Object[] {dataId, group, tenantTmp}, CONFIG_ALL_INFO_ROW_MAPPER);
@ -1392,16 +1418,17 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Override
public List<ConfigInfoWrapper> listGroupKeyMd5ByPage(int pageNo, int pageSize) {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
String sqlCountRows = configInfoMapper.count(null);
MapperContext context = new MapperContext((pageNo - 1) * pageSize, pageSize);
MapperResult sqlFetchRows = configInfoMapper.listGroupKeyMd5ByPageFetchRows(context);
PaginationHelper<ConfigInfoWrapper> helper = createPaginationHelper();
try {
Page<ConfigInfoWrapper> page = helper.fetchPageLimit(sqlCountRows, sqlFetchRows.getSql(),
sqlFetchRows.getParamList().toArray(), pageNo, pageSize, CONFIG_INFO_WRAPPER_ROW_MAPPER);
Page<ConfigInfoWrapper> page = helper
.fetchPageLimit(sqlCountRows, sqlFetchRows.getSql(), sqlFetchRows.getParamList().toArray(), pageNo,
pageSize, CONFIG_INFO_WRAPPER_ROW_MAPPER);
return page.getPageItems();
} catch (CannotGetJdbcConnectionException e) {
@ -1429,13 +1456,12 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
public ConfigInfoWrapper queryConfigInfo(final String dataId, final String group, final String tenant) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
return this.jt.queryForObject(configInfoMapper.select(
Arrays.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "type",
"gmt_modified", "md5", "encrypted_data_key"),
Arrays.asList("data_id", "group_id", "tenant_id")), new Object[] {dataId, group, tenantTmp},
CONFIG_INFO_WRAPPER_ROW_MAPPER);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
return this.jt.queryForObject(configInfoMapper.select(Arrays
.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "type", "gmt_modified",
"md5", "encrypted_data_key"), Arrays.asList("data_id", "group_id", "tenant_id")),
new Object[] {dataId, group, tenantTmp}, CONFIG_INFO_WRAPPER_ROW_MAPPER);
} catch (EmptyResultDataAccessException e) {
return null;
} catch (CannotGetJdbcConnectionException e) {
@ -1448,8 +1474,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
public List<ConfigAllInfo> findAllConfigInfo4Export(final String dataId, final String group, final String tenant,
final String appName, final List<Long> ids) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext();
if (!CollectionUtils.isEmpty(ids)) {
context.putWhereParameter(FieldConstant.IDS, ids);
@ -1467,8 +1493,8 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
}
MapperResult mapperResult = configInfoMapper.findAllConfigInfo4Export(context);
try {
return this.jt.query(mapperResult.getSql(), mapperResult.getParamList().toArray(),
CONFIG_ALL_INFO_ROW_MAPPER);
return this.jt
.query(mapperResult.getSql(), mapperResult.getParamList().toArray(), CONFIG_ALL_INFO_ROW_MAPPER);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
throw e;
@ -1482,11 +1508,11 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
}
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
return this.jt.query(
configInfoMapper.select(Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "type"),
Collections.singletonList("tenant_id")), new Object[] {tenantTmp},
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
return this.jt.query(configInfoMapper
.select(Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "type"),
Collections.singletonList("tenant_id")), new Object[] {tenantTmp},
CONFIG_INFO_WRAPPER_ROW_MAPPER);
} catch (EmptyResultDataAccessException e) { // Indicates that the data does not exist, returns null.
return Collections.EMPTY_LIST;
@ -1500,17 +1526,18 @@ public class ExternalConfigInfoPersistServiceImpl implements ConfigInfoPersistSe
@Deprecated
public Page<ConfigInfoBase> findAllConfigInfoBase(final int pageNo, final int pageSize) {
final int startRow = (pageNo - 1) * pageSize;
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
ConfigInfoMapper configInfoMapper = mapperManager
.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO);
String sqlCountRows = configInfoMapper.count(null);
MapperResult sqlFetchRows = configInfoMapper.findAllConfigInfoBaseFetchRows(
new MapperContext(startRow, pageSize));
MapperResult sqlFetchRows = configInfoMapper
.findAllConfigInfoBaseFetchRows(new MapperContext(startRow, pageSize));
PaginationHelper<ConfigInfoBase> helper = createPaginationHelper();
try {
return helper.fetchPageLimit(sqlCountRows, sqlFetchRows.getSql(), sqlFetchRows.getParamList().toArray(),
pageNo, pageSize, CONFIG_INFO_BASE_ROW_MAPPER);
return helper
.fetchPageLimit(sqlCountRows, sqlFetchRows.getSql(), sqlFetchRows.getParamList().toArray(), pageNo,
pageSize, CONFIG_INFO_BASE_ROW_MAPPER);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e, e);
throw e;