[ISSUE #9943] Resolve issue Config cas update not work (#9952)

* fix issue 9943

* add some description

* add some log for roll back transaction

* add some log for roll back transaction

* set default rollBackOnUpdateFail false.

* format style

* fix some bug
This commit is contained in:
Karson 2023-03-03 11:14:08 +08:00 committed by GitHub
parent acc8cbe698
commit e58c4441f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 29 deletions

View File

@ -28,6 +28,7 @@ import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.IllegalTransactionStateException;
import org.springframework.transaction.support.TransactionTemplate;
import java.util.List;
@ -210,16 +211,23 @@ public interface BaseDatabaseOperate extends DatabaseOperate {
*/
default Boolean update(TransactionTemplate transactionTemplate, JdbcTemplate jdbcTemplate,
List<ModifyRequest> contexts, BiConsumer<Boolean, Throwable> consumer) {
return transactionTemplate.execute(status -> {
boolean updateResult = Boolean.FALSE;
try {
updateResult = transactionTemplate.execute(status -> {
String[] errSql = new String[] {null};
Object[][] args = new Object[][] {null};
try {
contexts.forEach(pair -> {
errSql[0] = pair.getSql();
args[0] = pair.getArgs();
boolean rollBackOnUpdateFail = pair.isRollBackOnUpdateFail();
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "current sql : {}", errSql[0]);
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "current args : {}", args[0]);
jdbcTemplate.update(pair.getSql(), pair.getArgs());
int row = jdbcTemplate.update(pair.getSql(), pair.getArgs());
if (rollBackOnUpdateFail && row < 1) {
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "SQL update affected {} rows ", row);
throw new IllegalTransactionStateException("Illegal transaction");
}
});
if (consumer != null) {
consumer.accept(Boolean.TRUE, null);
@ -235,11 +243,18 @@ public interface BaseDatabaseOperate extends DatabaseOperate {
FATAL_LOG.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString());
throw e;
} catch (DataAccessException e) {
FATAL_LOG.error("[db-error] DataAccessException sql : {}, args : {}, error : {}", errSql[0], args[0],
ExceptionUtil.getAllExceptionMsg(e));
FATAL_LOG.error("[db-error] DataAccessException sql : {}, args : {}, error : {}", errSql[0],
args[0], ExceptionUtil.getAllExceptionMsg(e));
throw e;
}
});
} catch (IllegalTransactionStateException e) {
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "Roll back transaction for {} ", e.getMessage());
if (consumer != null) {
consumer.accept(Boolean.FALSE, e);
}
}
return updateResult;
}
/**

View File

@ -1794,7 +1794,7 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
use, effect, type, schema, configInfo.getDataId(), configInfo.getGroup(), tenantTmp,
configInfo.getMd5()};
EmbeddedStorageContextUtils.addSqlContext(sql, args);
EmbeddedStorageContextUtils.addSqlContext(true, sql, args);
}
@Override

View File

@ -56,6 +56,24 @@ public class EmbeddedStorageContextUtils {
SQL_CONTEXT.set(requests);
}
/**
* Add sql context.
*
* @param rollbackOnUpdateFail roll back when update fail
* @param sql sql
* @param args argument list
*/
public static void addSqlContext(boolean rollbackOnUpdateFail, String sql, Object... args) {
ArrayList<ModifyRequest> requests = SQL_CONTEXT.get();
ModifyRequest context = new ModifyRequest();
context.setExecuteNo(requests.size());
context.setSql(sql);
context.setArgs(args);
context.setRollBackOnUpdateFail(rollbackOnUpdateFail);
requests.add(context);
SQL_CONTEXT.set(requests);
}
/**
* Put extend info.
*

View File

@ -33,6 +33,8 @@ public class ModifyRequest implements Serializable {
private String sql;
private boolean rollBackOnUpdateFail = Boolean.FALSE;
private Object[] args;
public ModifyRequest() {
@ -66,6 +68,14 @@ public class ModifyRequest implements Serializable {
this.args = args;
}
public boolean isRollBackOnUpdateFail() {
return rollBackOnUpdateFail;
}
public void setRollBackOnUpdateFail(boolean rollBackOnUpdateFail) {
this.rollBackOnUpdateFail = rollBackOnUpdateFail;
}
@Override
public String toString() {
return "SQL{" + "executeNo=" + executeNo + ", sql='" + sql + '\'' + ", args=" + Arrays.toString(args) + '}';