title: How to avoid duplicate insert in DB through Java?
body: <p>I need to insert employee in Employee table What i want is  is to avoid duplicate inserts i.e. if thwo thread tries to insert same employee at same time then last transaction
  should fail. For example if first_name and hire_date is same for two employees(same employee coming from two threads) then fail the last transaction.</p>
  
  <p><strong>Approach 1:-</strong> First approach i can think of put the constraint at column level(like combined unique constraint on first_name and hire_date) or in the query check if
  employee exist throw error(i believe it will be possible through PL/SQL)</p>
  
  <p><strong>Approach 2:-</strong> Can it be done at java level too like create a method which first check if employee exists then throw error. In that case i need to make the method scynchronized (or
  synchronized block) but it will impact performance it will unnecassrily hold other transactions also.  Is there a way i can make put the lock(Reentrant lock) or use the synchronized method  based on name/hiredate so that only those specific thransaction are put on hold which has same name and hiredate</p>
  
  <pre><code>public void  save(Employee emp){
      //hibernate api to save
  }
  </code></pre>
  
  <p>I believe Approach 1 should be preferred as its simple and easier to implement. Right ? Even yes, i would like to know if it can be handled efficiently at java level ?</p>