Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BatchUpdateException: the batch will not terminate

I have an application which processes a very large file and sends data to an oracle database (using Java 6, oracle 9).

In a loop, I use a PreparedStatement ps and create all SQL statements generated with ps.addBatch().

I have a situation where a BatchUpdateException bue is thrown somewhere during the ps.executeBatch(). At that point, the batch stops to be executed.

I'd like the batch execution to continue, so that I can then check on failed updates in a method processUpdateCounts(bue.getUpdateCounts()).

The javadoc about class BatchUpdateException says:

After a command in a batch update fails to execute properly and a BatchUpdateException is thrown, the driver may or may not continue to process the remaining commands in the batch.

Is there a way to enforce continuation or do I need to alter my program so that it will execute the statement individually?

like image 849
transient_loop Avatar asked Jan 18 '11 14:01

transient_loop


People also ask

What is Java SQL BatchUpdateException?

In addition to the information provided by SQLException , a BatchUpdateException provides the update counts for all commands that were executed successfully during the batch update, that is, all commands that were executed before the error occurred.


2 Answers

Just found this link: JDBC Batch Update Problem

Apparently, it says there there is

NO WAY WITH ORACLE BATCH JDBC to proceed after first failure,

thus I am resorting to sending the inserts one by one. Thank you

(sorry for not looking better to find the link above before).

like image 71
transient_loop Avatar answered Oct 10 '22 16:10

transient_loop


there is a workaround that would allow you to use the batch feature. Instead of executing a simple INSERT statement, you can execute a PL/SQL block that will deal with the error appropriately:

BEGIN
   INSERT INTO your_table VALUES (?,?,...?);
EXCEPTION
   WHEN OTHERS THEN
      /* deal with the error. For example, log the error id and error msg 
         so that you can list them after the batch */
      INSERT INTO error_table VALUES (?, sqlerrm);
END

The performance should be on par with the batch insert (should be faster than individual execution of the statements). You could also call a stored procedure instead of a PL/SQL block.

like image 23
Vincent Malgrat Avatar answered Oct 10 '22 16:10

Vincent Malgrat