I'm using Java Prepared Statements Batch to insert multiple rows in to database. Sometimes in the batch there are two identical insert statements with the same primary key value and this throws an exception which is normal. My question is what happens with the data that was inserted before the exception? Should it be inserted (committed) or is rollback triggered? I can't find any documentation about this.
I have done some experiments with two identical SQL inserts in one batch. This is pseudocode:
int[] inserted;
PreparedStatement PreparedStatementInsert ... prepared with two identical SQL inserts.
try {
inserted = PreparedStatementInsert.executeBatch();
} catch (BatchUpdateException e) {
// program enters this part
inserted = e.getUpdateCounts();
}
In my experiment the catch block is executed and the inserted array has one element inserted[0] = 1. From documentation I have concluded that one (first) INSERT is done successfully and the second one is not. But when I look at the database it's empty as if no INSERT was done. Is there some documentation or explanation of this behavior? I'm using Java 6 with Postgres 9.0 database.