For my case multiple application uses data from db based on date, this data is date specific so if for a date data exist application should just read it otherwise it should fetch information from different site and do some calculations to create data for a date and store it in db and use it.

Since multiple applications require this data I'm looking for data in db by date if it exist simply read it otherwise fetch data from internet and after some processing store it in db with date.

But as same thing will be done by multiple application there are chances that first application comes and look for data in db for a date and founds no data so it start fetching this data from internet, in the meanwhile other application also looks for same date data but it also finds no data so it will also start fetching data and by the time its done other application would have already stored that information so second application would try to insert duplicate information in the table.

What is the best approach in this scenario to avoid duplicate ? Should checking just before insert or using transaction can help ?


You can add unique key constraint on the db table , the table itself will prevent the second application to insert duplicate data and return error code such as "unique key violation" , in the application you will need to handle this error/exception.

  • But how will I differentiate if there is some other database error from "unique key violation" in application. – techExplorer Dec 26 '15 at 3:37
  • 1
    you can catch each error and handle it , for example in Oracle : exception when dup_val_on_index then dbms_output.put_line('Cannot insert duplicate'); when others dbms_output.put_line('Other handling'); – alonk Dec 27 '15 at 6:47
  • we can handle it using exception but not sure if this is the most elegant way to do it – techExplorer Dec 28 '15 at 11:59

When one of your application initiates the process to fetch the data you can create a flag. Which ever process comes later can check for the same flag before starting the process.

  • Using flag will drastically reduce chances of duplication but will not completely eliminate it. – techExplorer Dec 26 '15 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.