Browse Source
This commit updates JbcTemplate#batchUpdate to provide additional information when one batch fails. Previously, the raw BatchUpdateException was thrown with no way to know what had completed thus far. This commit creates an AggregatedBatchUpdateException that wraps the original BatchUpdateException, yet providing the counters of the batches that ran prior to the exception. In essence, this represents the same state as the return value of the method if no batch fails. AggregateBatchUpdateException exposes the original BatchUpdateException in advanced case, such as checking for a sub-class that may contain additional information. Closes gh-23867pull/33351/head
3 changed files with 132 additions and 1 deletions
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/* |
||||
* Copyright 2002-2024 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.jdbc.core; |
||||
|
||||
import java.sql.BatchUpdateException; |
||||
|
||||
/** |
||||
* A {@link BatchUpdateException} that provides additional information about |
||||
* batches that were successful prior to one failing. |
||||
* |
||||
* @author Stephane Nicoll |
||||
* @since 6.2 |
||||
*/ |
||||
@SuppressWarnings("serial") |
||||
public class AggregatedBatchUpdateException extends BatchUpdateException { |
||||
|
||||
private final int[][] successfulUpdateCounts; |
||||
|
||||
private final BatchUpdateException originalException; |
||||
|
||||
/** |
||||
* Create an aggregated exception with the batches that have completed prior |
||||
* to the given {@code cause}. |
||||
* @param successfulUpdateCounts the counts of the batches that run successfully |
||||
* @param original the exception this instance aggregates |
||||
*/ |
||||
public AggregatedBatchUpdateException(int[][] successfulUpdateCounts, BatchUpdateException original) { |
||||
super(original.getMessage(), original.getSQLState(), original.getErrorCode(), |
||||
original.getUpdateCounts(), original.getCause()); |
||||
this.successfulUpdateCounts = successfulUpdateCounts; |
||||
this.originalException = original; |
||||
// Copy state of the original exception
|
||||
setNextException(original.getNextException()); |
||||
for (Throwable suppressed : original.getSuppressed()) { |
||||
addSuppressed(suppressed); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Return the batches that have completed successfully, prior to this exception. |
||||
* <p>Information about the batch that failed is available via |
||||
* {@link #getUpdateCounts()}. |
||||
* @return an array containing for each batch another array containing the numbers of |
||||
* rows affected by each update in the batch |
||||
* @see #getUpdateCounts() |
||||
*/ |
||||
public int[][] getSuccessfulUpdateCounts() { |
||||
return this.successfulUpdateCounts; |
||||
} |
||||
|
||||
/** |
||||
* Return the original {@link BatchUpdateException} that this exception aggregates. |
||||
* @return the original exception |
||||
*/ |
||||
public BatchUpdateException getOriginalException() { |
||||
return this.originalException; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue