JDBC batch批量Statement executeBatch 詳細解釋


 

JDBC它提供了一個數據庫batch能夠處理,在大容量數據業務(額外、刪除等。)的情況下,可以顯著提高系統性能。我有一個項目的聯系。在沒有使用batch治療,刪除5大概半個小時萬條數據,進行改造,採用了batch處理的方式。刪除5萬條數據基本上不會超過1分鍾。看一段JDBC代碼:

// 關閉自己主動運行
con.setAutoCommit(false);
Statement stmt = con.createStatement();

stmt.addBatch("INSERT INTO employees VALUES (1000, 'Joe Jones')");
stmt.addBatch("INSERT INTO departments VALUES (260, 'Shoe')");
stmt.addBatch("INSERT INTO emp_dept VALUES (1000, 260)");

// 提交一批要運行的更新命令
int[] updateCounts = stmt.executeBatch();

    本例中禁用了自己主動運行模式,從而在調用 Statement.executeBatch() 時可以防止 JDBC 運行事務處理。

禁用自己主動運行使得應用程序可以在錯誤發生及批處理中的某些命令不能運行時決定是否運行事務處理。

因此,當進行批處理更新時。通常應該關閉自己主動運行。

    在JDBC 2.0 中。Statement 對象可以記住可以一起提交運行的命令列表。創建語句時,與它關聯的命令列表為空。Statement.addBatch() 方法為調用語句的命令列表加入一個元素。假設批處理中包括有試圖返回結果集的命令,則當調用 Statement. executeBatch() 時。將拋出 SQLException。僅僅有 DDL 和 DML 命令(它們僅僅返回簡單的更新計數)才干作為批處理的一部分來運行。假設應用程序決定不提交已經為某語句構
造的命令批處理,則能夠調用方法 Statement.clearBatch()(以上沒有顯示)來又一次設置批處理。

    Statement.executeBatch() 方法將把命令批處理提交給基本 DBMS 來運行。

命令的運行將按照在批處理中的加入順序來進行。

ExecuteBatch() 為運行的命令返回更新計數數組。數組中相應於批處理中的每一個命令都包括了一項。而數組中各元素根據命令的運行順序(這還是和命令的最初加入順序同樣)來排序。調用executeBatch() 將關閉發出調用的 Statement 對象的當前結果集(假設有一個結果集是打開的)。executeBatch() 返回后,將又一次將語句的內部批處理命令列表設置為空。

    假設批處理中的某個命令無法正確運行。則 ExecuteBatch() 將拋出BatchUpdateException。

能夠調用BatchUpdateException.getUpdateCounts() 方法來為批處理中成功運行的命令返回更新計數的整型數組。由於當有第一個命令返回錯誤時,Statement.executeBatch() 就中止,並且這些命令是根據它們在批處理中的加入順序而運行的。所以假設 BatchUpdateException.getUpdateCounts() 所返回的數組包括 N 個元素,這就意味着在調用 executeBatch() 時批處理中的前 N 個命令被成功運行。用PreparedStatement 能夠象以下這樣寫代碼:


// 關閉自己主動運行
con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement("INSERT INTO employees VALUES (?, ?)");

stmt.setInt(1, 2000);
stmt.setString(2, "Kelly Kaufmann");
stmt.addBatch();

// 提交要運行的批處理
int[] updateCounts = stmt.executeBatch();

========================================

PrepareStatement 也是接口
PrepareStatement extends Statement
PrepareStatement 本身沒有 int[] executeBatch() throws SQLException 方法
而是繼承了Statement的方法,且它們都是接口沒有實際實現方法。但Statement
接口對executeBatch()方法做了規范
/**
     * Submits a batch of commands to the database for execution and
     * if all commands execute successfully, returns an array of update counts.
       每次提交一批命令到數據庫中運行,假設全部的命令都成功運行了,那么返回一個
       數組。這個數組是說明每條命令所影響的行數
     * The <code>int</code> elements of the array that is returned are ordered
     * to correspond to the commands in the batch, which are ordered
     * according to the order in which they were added to the batch.
       返回的數組中每一個整型值都是排過序的,它們的順序和批量處理中的命令們是一致的,
       命令的順序是依照它們被加到批處理中的順序一致。
     * The elements in the array returned by the method <code>executeBatch</code>
     * may be one of the following:
       executeBatch方法返回的數組中的元素可能是以下幾種情形之中的一個:
     * <OL>
     * <LI>A number greater than or equal to zero -- indicates that the
     * command was processed successfully and is an update count giving the
     * number of rows in the database that were affected by the command's
     * execution
       一個大於或等於零的數字。簡單說來命令成功運行后就返回它所影響到的行的數目
     * <LI>A value of <code>SUCCESS_NO_INFO</code> -- indicates that the command was
     * processed successfully but that the number of rows affected is
     * unknown
       
      * The constant indicating that a batch statement executed successfully
      * but that no count of the number of rows it affected is available.
      int SUCCESS_NO_INFO = -2;
      常量SUCCESS_NO_INFO代表的值=-2,也就是說命令運行成功了但命令影響到的行數
      無法統計,是未知的,僅僅能返回SUCCESS_NO_INFO來說明命令運行情況。
     * <P> * If one of the commands in a batch update fails to execute properly,
     * this method throws a <code>BatchUpdateException</code>, and a JDBC
     * driver may or may not continue to process the remaining commands in
     * the batch.
       假設批量處理時當中一個命令運行失敗,則會拋出一個異常BatchUpdateException
       JDBC驅動可能會停止剩余的命令,也可能繼續運行剩余的命令。


     * However, the driver's behavior must be consistent with a
     * particular DBMS, either always continuing to process commands or never
     * continuing to process commands.
       無論如何,驅動要怎么做取決於數據庫管理系統的細節。總是運行或總是不運行兩者其一。


     * If the driver continues processing
     * after a failure, the array returned by the method
     * <code>BatchUpdateException.getUpdateCounts</code>
     * will contain as many elements as there are commands in the batch, and
     * at least one of the elements will be the following:
       發生失敗后假設驅動繼續運行,通過BatchUpdateException.getUpdateCounts()方法返回
       的數組應該包含批處理中有的那些命令的結果,而且至少有一個元素的值是以下的情況:
     * <P>
     * <LI>A value of <code>EXECUTE_FAILED</code> -- indicates that the command failed
     * to execute successfully and occurs only if a driver continues to
     * process commands after a command fails
           int EXECUTE_FAILED = -3;
           指示命令沒有成功運行的常量值EXECUTE_FAILED,而且僅僅有在命令出錯后驅動繼續運行的情況下才會出現,
           假設出錯后不再運行,則返回的結果中沒有錯誤信息僅僅有那些被成功運行后的結果。
     * </OL>
     * <P> * A driver is not required to implement this method.
     * The possible implementations and return values have been modified in
     * the Java 2 SDK, Standard Edition, version 1.3 to
     * accommodate the option of continuing to proccess commands in a batch
     * update after a <code>BatchUpdateException</code> obejct has been thrown.
       驅動不實現此方法,可能會出現的實現和返回值在Java 2 SDK,Standard Edition,
       version 1.3 。以適應批處理時拋出BatchUpdateException 異常后是繼續運行還是
       終止運行的選項。
      
     * @return an array of update counts containing one element for each
     * command in the batch. The elements of the array are ordered according
     * to the order in which commands were added to the batch.
       返回一個和加入命令時的順序一樣的數組結果
     * @exception SQLException if a database access error occurs or the
     * driver does not support batch statements. Throws {@link BatchUpdateException}
     * (a subclass of <code>SQLException</code>) if one of the commands sent to the
     * database fails to execute properly or attempts to return a result set.
     * @since 1.3
     */
       假設數據庫訪問異常或驅動程序不支持批處理命令。或者,假設一個命令失敗或嘗試發送到數據庫來獲取結果集
       失敗,將拋出一個異常BatchUpdateException 這是SQLException子類。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM