title: Inserting or updating multiple records in database in a multi-threaded way in java
body: <p>I am updating multiple records in database. Now whenever UI sends the list of records to be updated, I have to just update those records in database. I am using JDBC template for that.</p>
  
  <p><strong>Earlier Case</strong></p>
  
  <p>Earlier what I was whenever I got records from UI, I just do </p>
  
  <pre><code>jdbcTemplate.batchUpdate(Query, List&lt;object[]&gt; params)
  </code></pre>
  
  <p>Whenever there was an exception, I used to rollback whole transaction. </p>
  
  <p>(<em>Updated</em> : Is batchUpdate multi-threaded or faster than batch update in some way?)</p>
  
  <p><strong>Later Case</strong></p>
  
  <p>But later as requirement changed whenever there was exception. So, whenever there is some exception, I should know which records failed to update. So I had to sent the records back to UI in case of exception with a reason, why did they failed.</p>
  
  <p>so I had to do something similar to this:</p>
  
  <pre><code>for(Record record : RecordList)
  {
     try{
         jdbcTemplate.update(sql, Object[] param)
     }catch(Exception ex){
         record.setReason("Exception : "+ex.getMessage());
         continue;
     }
  }
  </code></pre>
  
  <p>So am I doing this in right fashion, by using the loop?</p>
  
  <p>If yes, can someone suggest me how to make it multi-threaded.
  Or is there anything wrong in this case. 
  To be true, I was hesitating to use try catch block inside the loop :( . </p>
  
  <p>Please correct me, really need to learn a better way because I myself feel, there must be  a better way , thanks.</p>