File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and the fly likes problem with BatchUpdateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem with BatchUpdateException" Watch "problem with BatchUpdateException" New topic

problem with BatchUpdateException

rohan prakrit

Joined: Nov 18, 2004
Posts: 4
Hi Forum !

This problem has been quite perplexing for me !

Am using batchupdates in my code and i catch the BatchUpdateException to know the status for each batch that is being executed.

The code is something like this :

int[] output = pstmt.executeBatch();
catch(BatchUpdateException x)
int insertCount[] = x.getUpdateCounts();
for (int l = 0; l< insertCount.length; l++)
System.out.println("insert count ["+ l + "] = " + insertCount[l]);

The API says when BatchUpdateException.getUpdateCounts() returns -3 then it means the execution for that record failed.

My batch size is 10 and when any record in that batch fails then the output is something like this :

insert count [0] = -3
insert count [1] = -3
insert count [2] = -3
insert count [3] = -3
insert count [4] = -3
insert count [5] = -3
insert count [6] = -3
insert count [7] = -3 - // This is the record which has a problem
insert count [8] = -3
insert count [9] = -3

and this output makes me believe that all those 10 records failed to insert because of the "-3" output. Amazingly when i check the database 7 records have been inserted properly and only three of them failed.

If only 3 of them failed then why do all the 10 records give updatecount as "-3".

At this rate am not able to exactly tell how many records failed while batch updating/inserting !

It would be really helpful if you know the problem here.

Thanks Much Forum !
rohan prakrit

Joined: Nov 18, 2004
Posts: 4
Hi Again !

I just found this literature on the net :

According to the Oracle docs :
Oracle9i JDBC Developer's Guide and Reference
Release 2 (9.2) chapter 12
Performance Extensions

"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."

so does this mean, If i use prepared statements then i can't track how many records failed execution and how many succeeded is it ?

Dhamodharan Krishnan

Joined: Nov 07, 2001
Posts: 23
You can take the preparedStatement's update Count to see how many records have been successfully updated.
But then, the statement in each batch should update one row in the table, then only it will make sense, to see the number of rows updated and come to conclusion based on that.

Dhamodharan Krishnan,<br />Sun Microsystems, Bangalore<br />Direct : 91-80-4184 7247<br />Cell : 91-99 80 147 600
payal agrawal
Ranch Hand

Joined: Oct 11, 2005
Posts: 44
Look at the below URL.

How to exceute Batch process from PreparedStatement.
jQuery in Action, 2nd edition
subject: problem with BatchUpdateException
Similar Threads
Firing SQLs in Batch to enhance performance
BatchUpdateException: [IBM][CLI Driver][DB2/LINUX] SQL0302N
BatchUpdateException : getUpdateCounts doesn't work properly
BufferedReader help
batch update