zoukankan      html  css  js  c++  java
  • 关于JdbcTemplate批量更新batchUpdate()方法的返回值为-2

    原文:
    Update Counts in the Oracle Implementation of Standard Batching
    If a statement batch is processed successfully, then the integer array, or update counts array, returned by the statement executeBatch call will always have one element for each operation in the batch. In the Oracle implementation of standard update batching, the values of the array elements are as follows:

    For a prepared statement batch, it is not possible to know the number of rows affected in the database by each individual statement in the batch. Therefore, all array elements have a value of -2. According to the JDBC 2.0 specification, a value of -2 indicates that the operation was successful but the number of rows affected is unknown.

    For a generic statement batch, the array contains the actual update counts indicating the number of rows affected by each operation. The actual update counts can be provided only in the case of generic statements in the Oracle implementation of standard batching.

    For a callable statement batch, the server always returns the value 1 as the update count, irrespective of the number rows affected by each operation.

    In your code, upon successful processing of a batch, you should be prepared to handle either -2, 1, or true update counts in the array elements. For a successful batch processing, the array contains either all -2, 1, or all positive integers.

    Example 23-2 illustrates the use of standard update batching.

    Example 23-2 Standard Update Batching

    This example combines the sample fragments in the previous sections, accomplishing the following steps:

    Disabling auto-commit mode, which you should always do when using either update batching model

    Creating a prepared statement object

    Adding operations to the batch associated with the prepared statement object

    Processing the batch

    Committing the operations from the batch

    conn.setAutoCommit(false);
    
    PreparedStatement pstmt = 
              conn.prepareStatement("INSERT INTO employees VALUES(?, ?)");
    
    pstmt.setInt(1, 2000);
    pstmt.setString(2, "Milo Mumford");
    pstmt.addBatch();
    
    pstmt.setInt(1, 3000);
    pstmt.setString(2, "Sulu Simpson");
    pstmt.addBatch();
    
    int[] updateCounts = pstmt.executeBatch();
    
    conn.commit();
    
    pstmt.close();
    ...

    You can process the update counts array to determine if the batch processed successfully.

    Error Handling in the Oracle Implementation of Standard Batching
    If any one of the batched operations fails to complete successfully or attempts to return a result set during an executeBatch call, then the processing stops and a java.sql.BatchUpdateException is generated.

    After a batch exception, the update counts array can be retrieved using the getUpdateCounts method of the BatchUpdateException object. This returns an int array of update counts, just as the executeBatch method does. In the Oracle implementation of standard update batching, contents of the update counts array are as follows, after a batch is processed:

    For a prepared statement batch, it is not possible to know which operation failed. The array has one element for each operation in the batch, and each element has a value of -3. According to the JDBC 2.0 specification, a value of -3 indicates that an operation did not complete successfully. In this case, it was presumably just one operation that actually failed, but because the JDBC driver does not know which operation that was, it labels all the batched operations as failures.

    You should always perform a ROLLBACK operation in this situation.

    For a generic statement batch or callable statement batch, the update counts array is only a partial array containing the actual update counts up to the point of the error. The actual update counts can be provided because Oracle JDBC cannot use true batching for generic and callable statements in the Oracle implementation of standard update batching.

    For example, if there were 20 operations in the batch, the first 13 succeeded, and the 14th generated an exception, then the update counts array will have 13 elements, containing actual update counts of the successful operations.

    You can either commit or roll back the successful operations in this situation, as you prefer.

    In your code, upon failed processing of a batch, you should be prepared to handle either -3 or true update counts in the array elements when an exception occurs. For a failed batch processing, you will have either a full array of -3 or a partial array of positive integers.

    Intermixing Batched Statements and Nonbatched Statements
    You cannot call executeUpdate for regular, nonbatched processing of an operation if the statement object has a pending batch of operations.

    However, you can intermix batched operations and nonbatched operations in a single statement object if you process nonbatched operations either prior to adding any operations to the statement batch or after processing the batch. Essentially, you can call executeUpdate for a statement object only when its update batch is empty. If the batch is non-empty, then an exception will be generated.

    For example, it is valid to have a sequence, such as the following:

    ...
    PreparedStatement pstmt = 
              conn.prepareStatement("INSERT INTO employees VALUES(?, ?)");
    
    pstmt.setInt(1, 2000);
    pstmt.setString(2, "Milo Mumford");
    
    int scount = pstmt.executeUpdate();   // OK; no operations in pstmt batch
    
    pstmt.setInt(1, 3000);
    pstmt.setString(2, "Sulu Simpson");
    pstmt.addBatch();                    // Now start a batch
    
    pstmt.setInt(1, 4000);
    pstmt.setString(2, "Stan Leland");
    pstmt.addBatch();
    
    int[] bcounts = pstmt.executeBatch();
    
    pstmt.setInt(1, 5000);
    pstmt.setString(2, "Amy Feiner");
    
    int scount = pstmt.executeUpdate();   // OK; pstmt batch was executed
    ...

    Intermixing nonbatched operations on one statement object and batched operations on another statement object within your code is permissible. Different statement objects are independent of each other with regard to update batching operations. A COMMIT request will affect all nonbatched operations and all successful operations in processed batches, but will not affect any pending batches.

    译文:
    在标准批处理的Oracle实现中更新计数
    如果语句批处理成功,那么由语句executeBatch调用返回的整数数组或update计数数组将始终为批处理中的每个操作提供一个元素。
    在标准更新批处理的Oracle实现中,数组元素的值如下:

    对于准备好的语句批处理,不可能知道该批处理中的每个语句对数据库中影响的行数。
    因此,所有数组元素的值都是-2。
    根据JDBC 2.0规范,值为-2表示操作成功,但受影响的行数是未知的。

    对于一般的语句批处理,该数组包含实际的update计数,指示每个操作所影响的行数。
    只有在标准批处理的Oracle实现中,才可以提供实际的更新计数。

    对于可调用的语句批处理,服务器总是返回值1作为更新计数,而不考虑每个操作所影响的行数。

    在您的代码中,在成功处理一个批处理时,您应该准备好处理数组元素中的-2、1或真正的更新计数。
    对于成功的批处理,该数组包含了所有的-2、1或所有正整数。

    示例23-2演示了标准更新批处理的使用。

    示例23-2标准更新批处理

    本例结合了前几节中的示例片段,完成以下步骤:

    禁用自动提交模式,当使用更新批处理模型时,您应该总是这样做

    创建一个准备好的语句对象

    将操作添加到与准备好的语句对象关联的批处理中

    处理批处理

    从批处理中提交操作

    conn.setAutoCommit(false);
    
    PreparedStatement pstmt = 
              conn.prepareStatement("INSERT INTO employees VALUES(?, ?)");
    
    pstmt.setInt(1, 2000);
    pstmt.setString(2, "Milo Mumford");
    pstmt.addBatch();
    
    pstmt.setInt(1, 3000);
    pstmt.setString(2, "Sulu Simpson");
    pstmt.addBatch();
    
    int[] updateCounts = pstmt.executeBatch();
    
    conn.commit();
    
    pstmt.close();
    ...

    您可以处理更新计数数组,以确定批处理是否成功处理。

    标准批处理的Oracle实现中的错误处理
    如果任何一个批处理操作失败了,或者在一个executeBatch调用过程中尝试返回一个结果集,那么处理将停止,一个java.sql。
    BatchUpdateException生成。

    在批处理异常之后,可以使用BatchUpdateException对象的getupdatec打捞方法检索更新计数数组。
    这将返回一个int数组的update计数,就像executeBatch方法所做的那样。
    在标准更新批处理的Oracle实现中,更新计数数组的内容如下,在处理一批批处理后:

    对于准备好的语句批处理,不可能知道哪个操作失败了。
    该数组在批处理中每个操作都有一个元素,每个元素的值为-3。
    根据JDBC 2.0规范,3-3的值表明操作没有成功完成。
    在这种情况下,可能只有一个操作失败了,但是因为JDBC驱动程序不知道是哪个操作,所以它将所有的批处理操作都标记为失败。

    在这种情况下,您应该始终执行回滚操作。

    对于一个通用的语句批处理或可调用的语句批处理,update计数数组只是一个包含实际更新计数到错误点的部分数组。
    可以提供实际的更新计数,因为Oracle JDBC不能在标准更新批处理的Oracle实现中使用真正的批处理和可调用的语句。

    例如,如果在批处理中有20个操作,第13个成功,第14个生成一个异常,那么update计数数组将包含13个元素,其中包含成功操作的实际更新计数。

    您可以在这种情况下提交或回滚成功的操作,正如您所希望的那样。

    在您的代码中,当一个批处理失败时,您应该准备好在一个异常发生时,处理数组元素中的-3或真正的更新计数。
    对于一个失败的批处理,您将有一个完整的-3或一个正整数的部分数组。

    混合批处理语句和非批处理语句
    如果语句对象有一个待处理的操作,则不能调用executeUpdate来进行常规的、非批处理的操作。

    但是,如果您处理非批处理的操作,或者在将操作添加到语句批处理或处理批处理之前,您可以将批处理操作和非批处理操作混合在一个语句对象中。
    从本质上说,只有当它的更新批为空时,才可以为语句对象调用executeUpdate。
    如果批处理是非空的,则会生成一个异常。

    例如,有一个序列是有效的,例如以下:

    ...
    PreparedStatement pstmt = 
              conn.prepareStatement("INSERT INTO employees VALUES(?, ?)");
    
    pstmt.setInt(1, 2000);
    pstmt.setString(2, "Milo Mumford");
    
    int scount = pstmt.executeUpdate();   // OK; no operations in pstmt batch
    
    pstmt.setInt(1, 3000);
    pstmt.setString(2, "Sulu Simpson");
    pstmt.addBatch();                    // Now start a batch
    
    pstmt.setInt(1, 4000);
    pstmt.setString(2, "Stan Leland");
    pstmt.addBatch();
    
    int[] bcounts = pstmt.executeBatch();
    
    pstmt.setInt(1, 5000);
    pstmt.setString(2, "Amy Feiner");
    
    int scount = pstmt.executeUpdate();   // OK; pstmt batch was executed
    ...
    

    原文地址:
    https://docs.oracle.com/cd/B28359_01/java.111/b31224/oraperf.htm#CHDCBHEC

  • 相关阅读:
    java常量和变量的定义规则,变长参数的使用
    测试一下windowsLiveWriter
    对转换公式为LaTeX代码要注意什么
    后台登陆骨架
    为什么要把session存入数据库
    登录功能测试
    数据库快速配置
    一个小bug
    后台测试常需要的htm样式
    在分页的基础上添加删除和(查看,原理和删除一样)
  • 原文地址:https://www.cnblogs.com/lllini/p/11955258.html
Copyright © 2011-2022 走看看