I have a java 1.6 application which use batch insert for inserting records in Oracle db using jdbc driver. As you know on Statement object there is a method called executeBatch() which we use for batch updates. It has a return type of int array which has result of execution for each record in it. But it also throws BatchUpdateException in case of error and we can get result int array from that too. 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.
Note: Question is spesifically for 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. My question was about in what situation that can occur ?
This is the return javadoc of Statement.executeBatch() method. 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.
* @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. The execution of each them is to update 20 records, known in advance.
The execution of the batch of update statements occurs without a
BatchUpdateException
, or aSQLException
being thrown.If any of the elements in the returned int array is not 20 then you known there has been unexpected behaviour. This could be seen as a failure.
EDIT
From the JavaDoc of the BatchUpdateExcpetion (The highlights are my addition)
My understanding from this is that if any statement in the batch fails then a
BatchUpadteException
will be thrown.