View Javadoc
1   /**
2    *    Copyright 2009-2015 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    *       http://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
23   * during the execution of any nested batch.  The exception contains the
24   * java.sql.BatchUpdateException that is the root cause, as well as
25   * the results from any prior nested batch that executed successfully.  
26   * 
27   * @author Jeff Butler
28   */
29  public class BatchExecutorException extends ExecutorException {
30  
31    private static final long serialVersionUID = 154049229650533990L;
32    private final List<BatchResult> successfulBatchResults;
33    private final BatchUpdateException batchUpdateException;
34    private final BatchResult batchResult;
35  
36    public BatchExecutorException(String message, 
37                                  BatchUpdateException cause, 
38                                  List<BatchResult> successfulBatchResults,
39                                  BatchResult batchResult) {
40      super(message + " Cause: " + cause, cause);
41      this.batchUpdateException = cause;
42      this.successfulBatchResults = successfulBatchResults;
43      this.batchResult = batchResult;
44    }
45  
46    /*
47     * Returns the BatchUpdateException that caused the nested executor
48     * to fail.  That exception contains an array of row counts
49     * that can be used to determine exactly which statemtn of the
50     * executor caused the failure (or failures).
51     *
52     * @return the root BatchUpdateException
53     */
54    public BatchUpdateException getBatchUpdateException() {
55      return batchUpdateException;
56    }
57  
58    /*
59     * Returns a list of BatchResult objects.  There will be one entry
60     * in the list for each successful sub-executor executed before the failing
61     * executor.
62     *
63     * @return the previously successful executor results (may be an empty list
64     *         if no executor has executed successfully)
65     */
66    public List<BatchResult> getSuccessfulBatchResults() {
67      return successfulBatchResults;
68    }
69  
70    /*
71     * Returns the SQL statement that caused the failure
72     * (not the parameterArray)
73     *
74     * @return the failing SQL string
75     */
76    public String getFailingSqlStatement() {
77      return batchResult.getSql();
78    }
79  
80    /*
81     * Returns the statement id of the statement that caused the failure
82     *
83     * @return the statement id
84     */
85    public String getFailingStatementId() {
86      return batchResult.getMappedStatement().getId();
87    }
88  }