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