SylvainR
SylvainR

Reputation: 133

Spring batch DELETE BEFORE INSERT best practice?

In a Spring Batch job, i need to code a DELETE before an INSERT in order to avoid primary key violations on already existing keys.

What is the best practice regarding this need ? How and where should i implement this pre-requisited delete ?

Thx in advance for your suggestions. :-)

Upvotes: 1

Views: 1991

Answers (2)

Tarun
Tarun

Reputation: 182

Use tasklet to delete the data. For the tasklet to work, you may have to inject jdbcTemplate or similar to execute Db operations from tasklet. I also reserached this topic on internet and this is the best solution what I got till now.

Upvotes: 1

Pankaj Dubey
Pankaj Dubey

Reputation: 146

I believe you should insert the data in some temp table before deleting it from actual table because what if you delete record and insert fails?

The data will be lost or either use spring transactions to avoid this failure, in case something un-expected happens it roll-backs the whole transaction.

This is something like how we did for safe file writing.

Upvotes: 2

Related Questions