1 /*
2 * Copyright 2004-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 com.ibatis.sqlmap.engine.execution;
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. This exception is only thrown from the executeBatchDetailed
25 * method.
26 *
27 * @author Jeff Butler
28 */
29 public class BatchException extends Exception {
30
31 /** The successful batch results. */
32 private List successfulBatchResults;
33
34 /** The batch update exception. */
35 private BatchUpdateException batchUpdateException;
36
37 /** The failing sql statement. */
38 private String failingSqlStatement;
39
40 /** The failing statement id. */
41 private String failingStatementId;
42
43 /**
44 * Instantiates a new batch exception.
45 *
46 * @param message
47 * the message
48 * @param cause
49 * the cause
50 * @param successfulBatchResults
51 * the successful batch results
52 * @param failingStatementId
53 * the failing statement id
54 * @param failingSqlStatement
55 * the failing sql statement
56 */
57 public BatchException(String message, BatchUpdateException cause, List successfulBatchResults,
58 String failingStatementId, String failingSqlStatement) {
59 super(message, cause);
60 this.batchUpdateException = cause;
61 this.successfulBatchResults = successfulBatchResults;
62 this.failingStatementId = failingStatementId;
63 this.failingSqlStatement = failingSqlStatement;
64 }
65
66 /**
67 * Returns the BatchUpdateException that caused the nested batch to fail. That exception contains an array of row
68 * counts that can be used to determine exactly which statemtn of the batch caused the failure (or failures).
69 *
70 * @return the root BatchUpdateException
71 */
72 public BatchUpdateException getBatchUpdateException() {
73 return batchUpdateException;
74 }
75
76 /**
77 * Returns a list of BatchResult objects. There will be one entry in the list for each successful sub-batch executed
78 * before the failing batch.
79 *
80 * @return the previously successful batch results (may be an empty list if no batch has executed successfully)
81 */
82 public List getSuccessfulBatchResults() {
83 return successfulBatchResults;
84 }
85
86 /**
87 * Returns the SQL statement that caused the failure (not the parameters).
88 *
89 * @return the failing SQL string
90 */
91 public String getFailingSqlStatement() {
92 return failingSqlStatement;
93 }
94
95 /**
96 * Returns the statement id of the statement that caused the failure.
97 *
98 * @return the statement id
99 */
100 public String getFailingStatementId() {
101 return failingStatementId;
102 }
103 }