我有一個java 1.6應用程序,它使用批量插入在Oracle數據庫中使用jdbc驅動程序插入記錄。正如您在Statement對象中所瞭解的,有一個名爲executeBatch()的方法,我們將其用於批量更新。它有一個返回類型的int數組,其中有每個記錄的執行結果。但是如果出現錯誤,它也會拋出BatchUpdateException,我們也可以從中得到結果int數組。我的問題是在什麼樣的錯誤情況下,我應該期待BatchUpdateException,並且當我期待沒有拋出異常,但是對於某些記錄我會失敗。executeBatch在部分失敗的情況下的行爲
注意:問題特別針對Oracle JDBC。 爲了更清楚地說明,我已經看到在執行executeBatch()之後,我沒有得到BatchUpdateException,但是有些insert語句失敗。我的問題是關於在什麼情況下可能發生?
這是Statement.executeBatch()方法的返回javadoc。根據一般意見,當一個條目失敗時,執行拋出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;
定義「對於我失敗的某些記錄」。例如,如果您進行更新,那麼沒有受影響的行不是失敗,它只是意味着沒有行被更新。 –
這裏我只是插入語句。所以我沒有看到數據庫中的一些記錄,但我沒有看到任何異常跟蹤。 – cacert
@cacert,你的插入有一個where子句,防止雙重插入?我在想'像WHERE NOT EXISTS(...)' –