refactor: 将中断控制逻辑内化到 BatchUpdateResult 中
- BatchUpdateResult 新增 quietly 字段,由 recordErrorBatch() 自行判断 状态设置为 INTERRUPTED 或 COMPLETED_WITH_ERRORS - 移除 interrupt() 方法,消除 recordErrorBatch → interrupt 的时序耦合 - 外部调用方仅需 break 控制循环,不再干预结果对象的状态流转
This commit is contained in:
@@ -51,6 +51,11 @@ public class BatchUpdateResult {
|
||||
*/
|
||||
private final int batchSize;
|
||||
|
||||
/**
|
||||
* 是否静默模式
|
||||
*/
|
||||
private final boolean quietly;
|
||||
|
||||
/**
|
||||
* 本次分批更新的状态
|
||||
*/
|
||||
@@ -75,10 +80,11 @@ public class BatchUpdateResult {
|
||||
*/
|
||||
private int completeBatchCount;
|
||||
|
||||
BatchUpdateResult(int total, int batchCount, int batchSize) {
|
||||
BatchUpdateResult(int total, int batchCount, int batchSize, boolean quietly) {
|
||||
this.total = total;
|
||||
this.batchCount = batchCount;
|
||||
this.batchSize = batchSize;
|
||||
this.quietly = quietly;
|
||||
|
||||
this.allUpdateCounts = new HashMap<>(batchCount);
|
||||
this.allErrorsInfo = new HashMap<>(batchCount);
|
||||
@@ -101,17 +107,15 @@ public class BatchUpdateResult {
|
||||
this.allUpdateCounts.put(batchIndex, updateCounts);
|
||||
this.allErrorsInfo.put(batchIndex, new BatchUpdateErrorInfo(batchIndex, cause));
|
||||
if (this.status == BatchUpdateStatus.SUCCESS) {
|
||||
this.status = BatchUpdateStatus.COMPLETED_WITH_ERRORS;
|
||||
if (this.quietly) {
|
||||
this.status = BatchUpdateStatus.COMPLETED_WITH_ERRORS;
|
||||
}
|
||||
else {
|
||||
this.status = BatchUpdateStatus.INTERRUPTED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 中断
|
||||
*/
|
||||
void interrupt() {
|
||||
this.status = BatchUpdateStatus.INTERRUPTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定批次更新结果
|
||||
*
|
||||
|
||||
@@ -232,13 +232,13 @@ class JdbcOperationSupport {
|
||||
assertSqlNotNull(sql);
|
||||
checkArgument(batchSize > 0, "The batch size must be greater than 0.");
|
||||
if (params == null || params.isEmpty()) {
|
||||
return new BatchUpdateResult(0, 0, batchSize);
|
||||
return new BatchUpdateResult(0, 0, batchSize, quietly);
|
||||
}
|
||||
|
||||
final int paramsSize = params.size();
|
||||
final int batchCount = (paramsSize + batchSize - 1) / batchSize;
|
||||
|
||||
final BatchUpdateResult result = new BatchUpdateResult(paramsSize, batchCount, batchSize);
|
||||
final BatchUpdateResult result = new BatchUpdateResult(paramsSize, batchCount, batchSize, quietly);
|
||||
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
// 表示第几条数据,1, 2, 3, ..., paramsSize
|
||||
@@ -263,7 +263,6 @@ class JdbcOperationSupport {
|
||||
final int[] updateCounts = getUpdateCountsOnError(indexInBatch, e);
|
||||
result.recordErrorBatch(batchIndex, updateCounts, e);
|
||||
if (!quietly) {
|
||||
result.interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user