I'm using Oracle merge with jdbc template's batchupdate and it is inserting duplicates. However, the problem is it is not happening every time. It happened only for 150 items in a table of more than 2,00,000 items.
The query works properly when run in sqldeveloper, i'm suspecting the problem is in the way batch update is being performed.
String sql = "MERGE INTO XXX USING dual ON (column_one = ? ) " +
"WHEN NOT MATCHED THEN INSERT " +
"(column_one, column_two, column_three) " +
"VALUES (?,?,?)";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
AuditData data = requestData.get(i);
preparedStatement.setString(1, columnOne);
preparedStatement.setString(2, columnOne);
preparedStatement.setString(3, columnTwo);
preparedStatement.setString(4, columnThree);
}
@Override
public int getBatchSize() {
return requestData.size();
}
});