简体   繁体   English

部分失败时的executeBatch行为

[英]executeBatch behaviour in case of partial failure

I have a java 1.6 application which use batch insert for inserting records in Oracle db using jdbc driver.我有一个 java 1.6 应用程序,它使用批处理插入使用 jdbc 驱动程序在 Oracle db 中插入记录。 As you know on Statement object there is a method called executeBatch() which we use for batch updates.如您所知,Statement 对象有一个名为 executeBatch() 的方法,我们使用它进行批量更新。 It has a return type of int array which has result of execution for each record in it.它有一个 int 数组的返回类型,其中包含每个记录的执行结果。 But it also throws BatchUpdateException in case of error and we can get result int array from that too.但它也会在出错的情况下抛出 BatchUpdateException ,我们也可以从中获取结果 int 数组。 My question is in what error situations I should expect BatchUpdateException and when I should expect there is no exception thrown but for some records I get failure.我的问题是在什么错误情况下我应该期望 BatchUpdateException 以及什么时候我应该期望没有抛出异常但对于某些记录我会失败。

Note: Question is spesifically for Oracle JDBC.注意:问题特别针对 Oracle JDBC。 And to make it more clear, I have seen situations that after executing executeBatch() I did not get BatchUpdateException however some of the insert statements failed.为了更清楚地说明,我已经看到在执行 executeBatch() 之后我没有得到 BatchUpdateException 但是一些插入语句失败的情况。 My question was about in what situation that can occur ?我的问题是在什么情况下会发生?

This is the return javadoc of Statement.executeBatch() method.这是 Statement.executeBatch() 方法的返回 javadoc。 According to the general opinion here when one entry fails, execution throws BatchUpdateException then in which condition we can expect some entries in return array failed.根据这里的一般意见,当一个条目失败时,执行会抛出 BatchUpdateException 然后在这种情况下我们可以预期返回数组中的某些条目失败。

      * @return an array of update counts, with one entry for each command in the
 *         batch. The elements are ordered according to the order in which
 *         the commands were added to the batch.
 *         <p>
 *         <ol>
 *         <li> If the value of an element is >=0, the corresponding command
 *         completed successfully and the value is the update count for that
 *         command, which is the number of rows in the database affected by
 *         the command.</li>
 *         <li> If the value is SUCCESS_NO_INFO, the command completed
 *         successfully but the number of rows affected is unknown.
 *         <li>
 *         <li> If the value is EXECUTE_FAILED, the command failed.
 *         </ol>
 * @throws SQLException
 *             if an error occurs accessing the database
 */
public int[] executeBatch() throws SQLException;

Let's say that you have 5 batch update statements.假设您有 5 个批量更新语句。 The execution of each them is to update 20 records, known in advance.他们每个人的执行都是更新20条记录,事先知道。

The execution of the batch of update statements occurs without a BatchUpdateException , or a SQLException being thrown.一批更新语句的执行发生时没有BatchUpdateExceptionSQLException被抛出。

If any of the elements in the returned int array is not 20 then you known there has been unexpected behaviour.如果返回的 int 数组中的任何元素不是 20,那么您就知道出现了意外行为。 This could be seen as a failure.这可以被视为失败。

EDIT编辑

From the JavaDoc of the BatchUpdateExcpetion (The highlights are my addition)来自 BatchUpdateExcpetion 的JavaDoc (亮点是我的补充)

After a command in a batch update fails to execute properly and a BatchUpdateException is thrown, the driver may or may not continue to process the remaining commands in the batch.在批量更新中的命令无法正确执行并抛出 BatchUpdateException 后,驱动程序可能会也可能不会继续处理批处理中的剩余命令。 If the driver continues processing after a failure, the array returned by the method BatchUpdateException.getUpdateCounts will have an element for every command in the batch rather than only elements for the commands that executed successfully before the error.如果驱动程序在失败后继续处理,则 BatchUpdateException.getUpdateCounts 方法返回的数组将包含批处理中每个命令的元素,而不仅仅是错误前成功执行的命令的元素。 In the case where the driver stops [ed] processing commands, the array element for any command that failed is Statement.EXECUTE_FAILED.在驱动程序停止[ed]处理命令的情况下,任何失败命令的数组元素是 Statement.EXECUTE_FAILED。

My understanding from this is that if any statement in the batch fails then a BatchUpadteException will be thrown.我的理解是,如果批处理中的任何语句失败,则将抛出BatchUpadteException

The Oracle JDBC driver throws a BatchUpdateException if an error occurs in the middle of the batch.如果在批处理中间发生错误,Oracle JDBC 驱动程序将抛出 BatchUpdateException。

For example let's assume you're sending a batch with 10 entries (10 rows to insert in your case).例如,假设您要发送一个包含 10 个条目的批次(在您的案例中插入 10 行)。 Entries #0 through #4 are successful.条目#0 到#4 是成功的。 Entry #5 hits an error such as a primary key violation.条目 #5 遇到错误,例如主键违规。 The execution stops at 5 and the driver throws a BatchUpdateException.执行在 5 处停止,驱动程序抛出 BatchUpdateException。 If you call getUpdateCounts() you'll get an array of size 10 with 5 SUCCESS_NO_INFO and 5 EXECUTE_FAILED.如果您调用 getUpdateCounts(),您将获得一个大小为 10 的数组,其中包含 5 个 SUCCESS_NO_INFO 和 5 个 EXECUTE_FAILED。

Note that starting in 12c (database and driver) you can get an update count for each element of the batch.请注意,从 12c(数据库和驱动程序)开始,您可以获得批次中每个元素的更新计数。 This is more useful when you're executing updates in a batch.当您批量执行更新时,这更有用。 For each element in the batch you can know how many rows have been updated.对于批处理中的每个元素,您可以知道更新了多少行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM