title: How to lock on select and release lock after update is committed using spring?
body: <p>I have started using spring from last few months and I have a question on transactions. I have a java method inside my spring batch job which first does a select operation to get first 100 rows with status as 'NOT COMPLETED' and does a update on the selected rows to change the status to 'IN PROGRESS'. Since I'm processing around 10 million records, I want to run multiple instances of my batch job and each instance has multiple threads. For a single instance, to make sure two threads are not fetching the same set of records, I have made my method as synchonized. But if I run multiple instances of my batch job (multiple JVMs), there is high probability that same set of records might be fetched by both the instances even if I use "optimistic" or "pesimistic lock" or "select for update" since we cannot lock records during selection. Below is the example shown. Transaction 1 has fetched 100 records and meanwhile Transaction2 also fetched 100 records but if I enable locking transaction 2 waits until transaction 1 is updated and committed. But Transaction 2 again does the same update. </p>
  
  <p>Is there any way in spring to make transaction 2's select operation to wait until transaction 1's select is completed ? </p>
  
  <pre><code>Transaction1        Transaction2
  fetch 100 records   
                      fetch 100 records
  update 100 records
  
  
  
   commit         
                      update 100 records
                      commit
  
  
  
  @Transactional
  public synchronized List&lt;Student&gt; processStudentRecords(){
  List&lt;Student&gt; students = getNotCompletedRecords();
  if(null != students &amp;&amp; students.size() &gt; 0){
      updateStatusToInProgress(students);
  }
  return student;
  }
  </code></pre>
  
  <p>Note: I cannot perform update first and then select. I would appreciate if any alternative approach is suggested ?</p>