title: JPA Repository Spring Boot: Force Read from DB instead of cache
body: <p>We are using Spring JPARepository. We are having different batch process running <em>under separate VMs (separate applications, separate deployments</em>) and operating on the same underlying DB Table instance.</p>
  
  <p>Batch 1: <br>
      Step 1: Inserting in DB.Table1; <br>
      Step 2: Time consuming complex biz task; <br>
      Step 3: Updating the same row in DB.Table1;<br></p>
  
  <p>Batch 2: Updating same row in DB.Table1.</p>
  
  <p>When DB row is updated by Batch 2 process in between start/end of Batch 1, Batch 1 needs to read from DB and act accordingly. But instead it is reading from cache. 
              <code>Entity findById(Long id)</code></p>
  
  <p>Marking the entity <code>@Cacheable(false)</code> will impact all operation on this entity as a whole, which will be less performant and I don't want to do.</p>
  
  <p>What will be the other way of achieving the same at specific operation level?</p>
  
  <p><strong>EDIT</strong> - Is there any way, in this case, other than using Native SQL query to Force Read the LATEST data in Table1.Col1? The following didn't work:<br>1. Using <code>repository.saveAndFlush()</code> during Batch 1.Step 1 doesn't help in reading back the latest data Step 3 <code>repository.findById(id)</code><br> 2. <code>entityManager.refresh(entity)</code> didn't work <br> 3. Since each process is running under different context, using <code>LockModeType.PESSIMISTIC_FORCE_INCREMENT</code> on Batch 2.Step 1 won't help</p>