* fix #3617 * 调整代码格式 * 修改抛出的Exception类型 * 调整逻辑 * 移除没用到的方法 * 修改注释
This commit is contained in:
parent
0c194e37aa
commit
043a504a33
@ -32,6 +32,7 @@ import org.springframework.transaction.support.TransactionTemplate;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import static com.alibaba.nacos.config.server.utils.LogUtil.FATAL_LOG;
|
import static com.alibaba.nacos.config.server.utils.LogUtil.FATAL_LOG;
|
||||||
@ -196,6 +197,19 @@ public interface BaseDatabaseOperate extends DatabaseOperate {
|
|||||||
*/
|
*/
|
||||||
default Boolean update(TransactionTemplate transactionTemplate, JdbcTemplate jdbcTemplate,
|
default Boolean update(TransactionTemplate transactionTemplate, JdbcTemplate jdbcTemplate,
|
||||||
List<ModifyRequest> contexts) {
|
List<ModifyRequest> contexts) {
|
||||||
|
return update(transactionTemplate, jdbcTemplate, contexts, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* execute update operation, to fix #3617.
|
||||||
|
*
|
||||||
|
* @param transactionTemplate {@link TransactionTemplate}
|
||||||
|
* @param jdbcTemplate {@link JdbcTemplate}
|
||||||
|
* @param contexts {@link List} ModifyRequest list
|
||||||
|
* @return {@link Boolean}
|
||||||
|
*/
|
||||||
|
default Boolean update(TransactionTemplate transactionTemplate, JdbcTemplate jdbcTemplate,
|
||||||
|
List<ModifyRequest> contexts, BiConsumer<Boolean, Throwable> consumer) {
|
||||||
return transactionTemplate.execute(status -> {
|
return transactionTemplate.execute(status -> {
|
||||||
String[] errSql = new String[] {null};
|
String[] errSql = new String[] {null};
|
||||||
Object[][] args = new Object[][] {null};
|
Object[][] args = new Object[][] {null};
|
||||||
@ -207,10 +221,16 @@ public interface BaseDatabaseOperate extends DatabaseOperate {
|
|||||||
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "current args : {}", args[0]);
|
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "current args : {}", args[0]);
|
||||||
jdbcTemplate.update(pair.getSql(), pair.getArgs());
|
jdbcTemplate.update(pair.getSql(), pair.getArgs());
|
||||||
});
|
});
|
||||||
|
if (consumer != null) {
|
||||||
|
consumer.accept(Boolean.TRUE, null);
|
||||||
|
}
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
} catch (BadSqlGrammarException | DataIntegrityViolationException e) {
|
} catch (BadSqlGrammarException | DataIntegrityViolationException e) {
|
||||||
FATAL_LOG.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString());
|
FATAL_LOG.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString());
|
||||||
return false;
|
if (consumer != null) {
|
||||||
|
consumer.accept(Boolean.FALSE, e);
|
||||||
|
}
|
||||||
|
return Boolean.FALSE;
|
||||||
} catch (CannotGetJdbcConnectionException e) {
|
} catch (CannotGetJdbcConnectionException e) {
|
||||||
FATAL_LOG.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString());
|
FATAL_LOG.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString());
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -133,8 +133,20 @@ public interface DatabaseOperate {
|
|||||||
* @return is success
|
* @return is success
|
||||||
*/
|
*/
|
||||||
default Boolean blockUpdate() {
|
default Boolean blockUpdate() {
|
||||||
|
return blockUpdate(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* data modify transaction The SqlContext to be executed in the current thread will be executed and automatically
|
||||||
|
* cleared.
|
||||||
|
* @author klw(213539@qq.com)
|
||||||
|
* 2020/8/24 18:16
|
||||||
|
* @param consumer the consumer
|
||||||
|
* @return java.lang.Boolean
|
||||||
|
*/
|
||||||
|
default Boolean blockUpdate(BiConsumer<Boolean, Throwable> consumer) {
|
||||||
try {
|
try {
|
||||||
return update(EmbeddedStorageContextUtils.getCurrentSqlContext(), null);
|
return update(EmbeddedStorageContextUtils.getCurrentSqlContext(), consumer);
|
||||||
} finally {
|
} finally {
|
||||||
EmbeddedStorageContextUtils.cleanAllContext();
|
EmbeddedStorageContextUtils.cleanAllContext();
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package com.alibaba.nacos.config.server.service.repository.embedded;
|
package com.alibaba.nacos.config.server.service.repository.embedded;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
|
||||||
import com.alibaba.nacos.common.utils.MD5Utils;
|
import com.alibaba.nacos.common.utils.MD5Utils;
|
||||||
import com.alibaba.nacos.common.notify.NotifyCenter;
|
import com.alibaba.nacos.common.notify.NotifyCenter;
|
||||||
import com.alibaba.nacos.config.server.configuration.ConditionOnEmbeddedStorage;
|
import com.alibaba.nacos.config.server.configuration.ConditionOnEmbeddedStorage;
|
||||||
@ -70,6 +71,7 @@ import java.util.Map;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ADVANCE_INFO_ROW_MAPPER;
|
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ADVANCE_INFO_ROW_MAPPER;
|
||||||
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ALL_INFO_ROW_MAPPER;
|
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ALL_INFO_ROW_MAPPER;
|
||||||
@ -189,6 +191,12 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
|
|||||||
@Override
|
@Override
|
||||||
public void addConfigInfo(final String srcIp, final String srcUser, final ConfigInfo configInfo,
|
public void addConfigInfo(final String srcIp, final String srcUser, final ConfigInfo configInfo,
|
||||||
final Timestamp time, final Map<String, Object> configAdvanceInfo, final boolean notify) {
|
final Timestamp time, final Map<String, Object> configAdvanceInfo, final boolean notify) {
|
||||||
|
addConfigInfo(srcIp, srcUser, configInfo, time, configAdvanceInfo, notify, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addConfigInfo(final String srcIp, final String srcUser, final ConfigInfo configInfo,
|
||||||
|
final Timestamp time, final Map<String, Object> configAdvanceInfo, final boolean notify,
|
||||||
|
BiConsumer<Boolean, Throwable> consumer) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final String tenantTmp =
|
final String tenantTmp =
|
||||||
@ -205,7 +213,7 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
|
|||||||
configInfo.getTenant());
|
configInfo.getTenant());
|
||||||
insertConfigHistoryAtomic(hisId, configInfo, srcIp, srcUser, time, "I");
|
insertConfigHistoryAtomic(hisId, configInfo, srcIp, srcUser, time, "I");
|
||||||
EmbeddedStorageContextUtils.onModifyConfigInfo(configInfo, srcIp, time);
|
EmbeddedStorageContextUtils.onModifyConfigInfo(configInfo, srcIp, time);
|
||||||
databaseOperate.blockUpdate();
|
databaseOperate.blockUpdate(consumer);
|
||||||
} finally {
|
} finally {
|
||||||
EmbeddedStorageContextUtils.cleanAllContext();
|
EmbeddedStorageContextUtils.cleanAllContext();
|
||||||
}
|
}
|
||||||
@ -2291,6 +2299,12 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
|
|||||||
int skipCount = 0;
|
int skipCount = 0;
|
||||||
List<Map<String, String>> failData = null;
|
List<Map<String, String>> failData = null;
|
||||||
List<Map<String, String>> skipData = null;
|
List<Map<String, String>> skipData = null;
|
||||||
|
|
||||||
|
final BiConsumer<Boolean, Throwable> callFinally = (result, t) -> {
|
||||||
|
if (t != null) {
|
||||||
|
throw new NacosRuntimeException(0, t);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for (int i = 0; i < configInfoList.size(); i++) {
|
for (int i = 0; i < configInfoList.size(); i++) {
|
||||||
ConfigAllInfo configInfo = configInfoList.get(i);
|
ConfigAllInfo configInfo = configInfoList.get(i);
|
||||||
@ -2322,10 +2336,10 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
|
|||||||
}
|
}
|
||||||
configAdvanceInfo.put("type", type);
|
configAdvanceInfo.put("type", type);
|
||||||
try {
|
try {
|
||||||
addConfigInfo(srcIp, srcUser, configInfo2Save, time, configAdvanceInfo, notify);
|
addConfigInfo(srcIp, srcUser, configInfo2Save, time, configAdvanceInfo, notify, callFinally);
|
||||||
succCount++;
|
succCount++;
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
if (!StringUtils.contains("DuplicateKeyException", e.toString())) {
|
if (!StringUtils.contains(e.toString(), "DuplicateKeyException")) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
// uniqueness constraint conflict
|
// uniqueness constraint conflict
|
||||||
|
@ -128,7 +128,7 @@ public class StandaloneDatabaseOperateImpl implements BaseDatabaseOperate {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean update(List<ModifyRequest> modifyRequests, BiConsumer<Boolean, Throwable> consumer) {
|
public Boolean update(List<ModifyRequest> modifyRequests, BiConsumer<Boolean, Throwable> consumer) {
|
||||||
return update(modifyRequests);
|
return update(transactionTemplate, jdbcTemplate, modifyRequests, consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user