* 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:
parent
acc8cbe698
commit
e58c4441f4
@ -28,6 +28,7 @@ import org.springframework.jdbc.BadSqlGrammarException;
|
|||||||
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.jdbc.core.RowMapper;
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
|
import org.springframework.transaction.IllegalTransactionStateException;
|
||||||
import org.springframework.transaction.support.TransactionTemplate;
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -210,36 +211,50 @@ public interface BaseDatabaseOperate extends DatabaseOperate {
|
|||||||
*/
|
*/
|
||||||
default Boolean update(TransactionTemplate transactionTemplate, JdbcTemplate jdbcTemplate,
|
default Boolean update(TransactionTemplate transactionTemplate, JdbcTemplate jdbcTemplate,
|
||||||
List<ModifyRequest> contexts, BiConsumer<Boolean, Throwable> consumer) {
|
List<ModifyRequest> contexts, BiConsumer<Boolean, Throwable> consumer) {
|
||||||
return transactionTemplate.execute(status -> {
|
boolean updateResult = Boolean.FALSE;
|
||||||
String[] errSql = new String[] {null};
|
try {
|
||||||
Object[][] args = new Object[][] {null};
|
updateResult = transactionTemplate.execute(status -> {
|
||||||
try {
|
String[] errSql = new String[] {null};
|
||||||
contexts.forEach(pair -> {
|
Object[][] args = new Object[][] {null};
|
||||||
errSql[0] = pair.getSql();
|
try {
|
||||||
args[0] = pair.getArgs();
|
contexts.forEach(pair -> {
|
||||||
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "current sql : {}", errSql[0]);
|
errSql[0] = pair.getSql();
|
||||||
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "current args : {}", args[0]);
|
args[0] = pair.getArgs();
|
||||||
jdbcTemplate.update(pair.getSql(), pair.getArgs());
|
boolean rollBackOnUpdateFail = pair.isRollBackOnUpdateFail();
|
||||||
});
|
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "current sql : {}", errSql[0]);
|
||||||
if (consumer != null) {
|
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "current args : {}", args[0]);
|
||||||
consumer.accept(Boolean.TRUE, null);
|
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);
|
||||||
|
}
|
||||||
|
return Boolean.TRUE;
|
||||||
|
} catch (BadSqlGrammarException | DataIntegrityViolationException e) {
|
||||||
|
FATAL_LOG.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString());
|
||||||
|
if (consumer != null) {
|
||||||
|
consumer.accept(Boolean.FALSE, e);
|
||||||
|
}
|
||||||
|
return Boolean.FALSE;
|
||||||
|
} catch (CannotGetJdbcConnectionException e) {
|
||||||
|
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));
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
return Boolean.TRUE;
|
});
|
||||||
} catch (BadSqlGrammarException | DataIntegrityViolationException e) {
|
} catch (IllegalTransactionStateException e) {
|
||||||
FATAL_LOG.error("[db-error] sql : {}, args : {}, error : {}", errSql[0], args[0], e.toString());
|
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "Roll back transaction for {} ", e.getMessage());
|
||||||
if (consumer != null) {
|
if (consumer != null) {
|
||||||
consumer.accept(Boolean.FALSE, e);
|
consumer.accept(Boolean.FALSE, e);
|
||||||
}
|
|
||||||
return Boolean.FALSE;
|
|
||||||
} catch (CannotGetJdbcConnectionException e) {
|
|
||||||
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));
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
return updateResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1794,7 +1794,7 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
|
|||||||
use, effect, type, schema, configInfo.getDataId(), configInfo.getGroup(), tenantTmp,
|
use, effect, type, schema, configInfo.getDataId(), configInfo.getGroup(), tenantTmp,
|
||||||
configInfo.getMd5()};
|
configInfo.getMd5()};
|
||||||
|
|
||||||
EmbeddedStorageContextUtils.addSqlContext(sql, args);
|
EmbeddedStorageContextUtils.addSqlContext(true, sql, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -56,6 +56,24 @@ public class EmbeddedStorageContextUtils {
|
|||||||
SQL_CONTEXT.set(requests);
|
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.
|
* Put extend info.
|
||||||
*
|
*
|
||||||
|
@ -33,6 +33,8 @@ public class ModifyRequest implements Serializable {
|
|||||||
|
|
||||||
private String sql;
|
private String sql;
|
||||||
|
|
||||||
|
private boolean rollBackOnUpdateFail = Boolean.FALSE;
|
||||||
|
|
||||||
private Object[] args;
|
private Object[] args;
|
||||||
|
|
||||||
public ModifyRequest() {
|
public ModifyRequest() {
|
||||||
@ -66,6 +68,14 @@ public class ModifyRequest implements Serializable {
|
|||||||
this.args = args;
|
this.args = args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isRollBackOnUpdateFail() {
|
||||||
|
return rollBackOnUpdateFail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRollBackOnUpdateFail(boolean rollBackOnUpdateFail) {
|
||||||
|
this.rollBackOnUpdateFail = rollBackOnUpdateFail;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "SQL{" + "executeNo=" + executeNo + ", sql='" + sql + '\'' + ", args=" + Arrays.toString(args) + '}';
|
return "SQL{" + "executeNo=" + executeNo + ", sql='" + sql + '\'' + ", args=" + Arrays.toString(args) + '}';
|
||||||
|
Loading…
Reference in New Issue
Block a user