When I am executing executeBatch method in java, its returning an int array that\'s fine but the value is -2 of all array elements,
it should be 0 or +ve number that
Standard Update Batching...
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, in case of an error in between execution of the batch, the executeBatch method cannot return a value, instead it throws a BatchUpdateException. In this case, the exception itself carries an int array of size n as its data, where n is the number of successful record executions. For example, if the batch is of size 5 and the error occurs at the 4th record, then the BatchUpdateException has an array of size 3 (3 records executed successfully) and each item in the array represents how many rows were affected by each of them.
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.
The jdbc-spec has the following to say about the return-code of batch-updates:
■ 0 or greater — the command was processed successfully and the value is an update count indicating the number of rows in the database that were affected by the command’s execution Chapter 14 Batch Updates 121
■ Statement.SUCCESS_NO_INFO — the command was processed successfully, but the number of rows affected is unknown
Statement.SUCCESS_NO_INFO is defined as being -2, so your result says everything worked fine, but you won't get information on the number of updated columns.
The oracle-documentation states:
• 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.
So it seems if you need the update-counts you can't use PreparedStatement
s but have to fall back to plain Statement
s.
A value of -2 indicates that a element was processed successfully, but that the number of effected rows is unknown.
specs
Note that since Oracle 12c this shouldn't be the case anymore:
For a prepared statement batch, the array contains the actual update counts indicating the number of rows affected by each operation.
See https://docs.oracle.com/database/121/JJDBC/oraperf.htm#JJDBC28773