0

I need to insert or update records based on whether record already exist.

I am using JdbcBatchItemWriter to write the records. but if the record with the primary key already exist, I should update it...

So one solution is:

To make two seperate lists one for insert and one for update(note: I have to check in my processor every time whether the record already exist and thus add the record in one of the list), and have two different JdbcBatchItemWriter instances in my writer, for example:

JdbcBatchItemWriter<insertList> insertWriter;
JdbcBatchItemWriter<updateList> updateWriter;

Is there any other way to switch between the queries in writer based on record already exist at the time of batch update...i.e. just one

JdbcBatchItemWriter<mylist> allWriter...and 
allWriter.write(mylistallitems);

I am thinking of using a merge query...but are there any performance issues?

user2971387
  • 109
  • 1
  • 3
  • 11
  • possible duplicate of [update on duplicate key with JdbcBatchItemWriter](http://stackoverflow.com/questions/21532058/update-on-duplicate-key-with-jdbcbatchitemwriter) – Luca Basso Ricci Mar 05 '14 at 11:04
  • thanks for the answer, further I am thinking of using oracle merge for this, can you please tell if there will be any performane problems with using oracle merge – user2971387 Mar 05 '14 at 11:24

1 Answers1

0

Having two different lists may be a better option since if you have a different persistence mechanism in the future, you needn't redesign your app. You may want to have a single query to get all existing primary keys from DB, store it a Collection holder, and refer to it in the processor.

A brief search on SO on 'Oracle Merge Performance' indicates multiple instances of performance issues due to different factors & maybe slower than crafted insert/update SQLs.

Also if you are receiving the complete data again (for updates), you may want to consider the truncate-insert approach [delete before insertions by adding a listener]

ram
  • 747
  • 2
  • 11
  • 34