Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with BatchUpdateException

 
rohan prakrit
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :

try
{
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
 
rohan prakrit
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

-Rohan
 
Dhamodharan Krishnan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
payal agrawal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the below URL.

How to exceute Batch process from PreparedStatement.
http://www.java-tips.org/content/view/274/29/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic