BatchExecutorException.java

  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. import java.sql.BatchUpdateException;
  18. import java.util.List;

  19. /**
  20.  * This exception is thrown if a <code>java.sql.BatchUpdateException</code> is caught during the execution of any nested
  21.  * batch. The exception contains the java.sql.BatchUpdateException that is the root cause, as well as the results from
  22.  * any prior nested batch that executed successfully.
  23.  *
  24.  * @author Jeff Butler
  25.  */
  26. public class BatchExecutorException extends ExecutorException {

  27.   private static final long serialVersionUID = 154049229650533990L;
  28.   private final List<BatchResult> successfulBatchResults;
  29.   private final BatchUpdateException batchUpdateException;
  30.   private final BatchResult batchResult;

  31.   public BatchExecutorException(String message, BatchUpdateException cause, List<BatchResult> successfulBatchResults,
  32.       BatchResult batchResult) {
  33.     super(message + " Cause: " + cause, cause);
  34.     this.batchUpdateException = cause;
  35.     this.successfulBatchResults = successfulBatchResults;
  36.     this.batchResult = batchResult;
  37.   }

  38.   /**
  39.    * Returns the BatchUpdateException that caused the nested executor to fail. That exception contains an array of row
  40.    * counts that can be used to determine exactly which statement of the executor caused the failure (or failures).
  41.    *
  42.    * @return the root BatchUpdateException
  43.    */
  44.   public BatchUpdateException getBatchUpdateException() {
  45.     return batchUpdateException;
  46.   }

  47.   /**
  48.    * Returns a list of BatchResult objects. There will be one entry in the list for each successful sub-executor
  49.    * executed before the failing executor.
  50.    *
  51.    * @return the previously successful executor results (maybe an empty list if no executor has executed successfully)
  52.    */
  53.   public List<BatchResult> getSuccessfulBatchResults() {
  54.     return successfulBatchResults;
  55.   }

  56.   /**
  57.    * Returns the SQL statement that caused the failure (not the parameterArray).
  58.    *
  59.    * @return the failing SQL string
  60.    */
  61.   public String getFailingSqlStatement() {
  62.     return batchResult.getSql();
  63.   }

  64.   /**
  65.    * Returns the statement id of the statement that caused the failure.
  66.    *
  67.    * @return the statement id
  68.    */
  69.   public String getFailingStatementId() {
  70.     return batchResult.getMappedStatement().getId();
  71.   }
  72. }