1

I have a working method in Java that inserts many rows as a batch.

The issue I'm facing is that I have a unique constraint on my table and if there are some rows that are already in the table the method does not insert the ones that do not belong in the database. Instead it throws the following exception

java.sql.BatchUpdateException: Batch entry 0 INSERT INTO .... was aborted.  Call getNextException to see the cause.

Is there anything I can do to make the method insert the ones that are not already in table and not abort like it's doing now?

Arya
  • 8,473
  • 27
  • 105
  • 175

1 Answers1

0

You can handle this stuff through before insert trigger on that table.

Raman Shrivastava
  • 2,923
  • 15
  • 26
  • I was doing that before but with thousands of rows it slows down. There are no alternatives? – Arya Jun 25 '15 at 18:55
  • Based upon which field or combination of fields do you decide if that row is already their in db? Before your batch insert, you can get those through a prepared statement in java, then compare (and whatever logic you need), and send only those rows through which won't fail. – Raman Shrivastava Jun 25 '15 at 18:58
  • Only if you `LOCK` the table first. – Craig Ringer Jun 26 '15 at 02:34