BatchExecutorException.java

  1. /*
  2.  *    Copyright 2009-2022 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. import java.sql.BatchUpdateException;
  18. import java.util.List;

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

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

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

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

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

  63.   /**
  64.    * Returns the SQL statement that caused the failure
  65.    * (not the parameterArray).
  66.    *
  67.    * @return the failing SQL string
  68.    */
  69.   public String getFailingSqlStatement() {
  70.     return batchResult.getSql();
  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. }