title: Two threads adding new rows at the same time - how to prevent?
body: <p>In my application, I have couple of threads that execute some logic.
  At the end they adding new row to some table.</p>
  
  <p>Before adding the new row, they check if a previous entry with the same details does not already exist. If one found - they updating instead of adding.</p>
  
  <p>The problem is when some thread A do the check, see that no previous entity with the same details exist, and just before he add a new row, the thread B search the DB for the same entity. Thread B see that no such entity exist so he add new row too.</p>
  
  <p>The result is that there are two rows with the same data in the table.</p>
  
  <p>Note: no table key violated, because the thread get the next sequence just before adding the row, and the table key is some ID that does not related to the data.</p>
  
  <p>Even if I will change the table key so it will be a combination of the data, It will prevent two rows with the same data, but will cause a DB error when the second thread will try to add the row.</p>
  
  <p>Thank you in advance for the help, Roy. </p>