1 /*
2 * Copyright 2009-2023 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.ibatis.executor;
17
18 import java.sql.BatchUpdateException;
19 import java.util.List;
20
21 /**
22 * This exception is thrown if a <code>java.sql.BatchUpdateException</code> is caught during the execution of any nested
23 * batch. The exception contains the java.sql.BatchUpdateException that is the root cause, as well as the results from
24 * any prior nested batch that executed successfully.
25 *
26 * @author Jeff Butler
27 */
28 public class BatchExecutorException extends ExecutorException {
29
30 private static final long serialVersionUID = 154049229650533990L;
31 private final List<BatchResult> successfulBatchResults;
32 private final BatchUpdateException batchUpdateException;
33 private final BatchResult batchResult;
34
35 public BatchExecutorException(String message, BatchUpdateException cause, List<BatchResult> successfulBatchResults,
36 BatchResult batchResult) {
37 super(message + " Cause: " + cause, cause);
38 this.batchUpdateException = cause;
39 this.successfulBatchResults = successfulBatchResults;
40 this.batchResult = batchResult;
41 }
42
43 /**
44 * Returns the BatchUpdateException that caused the nested executor to fail. That exception contains an array of row
45 * counts that can be used to determine exactly which statement of the executor caused the failure (or failures).
46 *
47 * @return the root BatchUpdateException
48 */
49 public BatchUpdateException getBatchUpdateException() {
50 return batchUpdateException;
51 }
52
53 /**
54 * Returns a list of BatchResult objects. There will be one entry in the list for each successful sub-executor
55 * executed before the failing executor.
56 *
57 * @return the previously successful executor results (maybe an empty list if no executor has executed successfully)
58 */
59 public List<BatchResult> getSuccessfulBatchResults() {
60 return successfulBatchResults;
61 }
62
63 /**
64 * Returns the SQL statement that caused the failure (not the parameterArray).
65 *
66 * @return the failing SQL string
67 */
68 public String getFailingSqlStatement() {
69 return batchResult.getSql();
70 }
71
72 /**
73 * Returns the statement id of the statement that caused the failure.
74 *
75 * @return the statement id
76 */
77 public String getFailingStatementId() {
78 return batchResult.getMappedStatement().getId();
79 }
80 }