title: Spring data jpa get old result under multi threading
body: <p>Under multi threading, I keep getting old result from repository. </p>
  
  <pre><code>@Transactional(isolation = Isolation.REPEATABLE_READ)
  public void updateScore(int score, Long userId) {
      logger.info(RegularLock.getInstance().getLock().toString());
      synchronized (RegularLock.getInstance().getLock()) {
          Customer customer = customerDao.findOne(userId);
          System.out.println("start:": customer.getScore());
          customer.setScore(customer.getScore().subtract(score));
          customerDao.saveAndFlush(customer);
      }
  
  }
  </code></pre>
  
  <p>And CustomerDao looks like</p>
  
  <pre><code>@Transactional
  public T saveAndFlush(T model, Long id) {
      T res = repository.saveAndFlush(model);
      EntityManager manager = jpaContext.getEntityManagerByManagedType(model.getClass());
      manager.refresh(manager.find(model.getClass(), id));
      return res;
  }
  </code></pre>
  
  <p><code>saveAndFlush()</code> from <code>JpaRepository</code> is used in order to save the change instantly and the entire code is locked. But I still keep getting old result.</p>
  
  <pre><code>java.util.concurrent.locks.ReentrantLock@10a9598d[Unlocked]
  start:710
  java.util.concurrent.locks.ReentrantLock@10a9598d[Unlocked]
  start:710
  </code></pre>
  
  <p>I'm using springboot with spring data jpa.</p>
  
  <p>I put all code in a test controller, and the problem remains</p>
  
  <pre><code>@RestController
  @RequestMapping(value = "/test", produces = "application/json")
  public class TestController {
      private static Long testId;
  
      private final CustomerBalanceRepository repository;
  
      @Autowired
      public TestController(CustomerBalanceRepository repository) {
          this.repository = repository;
      }
  
      @PostConstruct
      public void init() {
  //        CustomerBalance customer = new CustomerBalance();
  //        repository.save(customer);
  //        testId = customer.getId();
      }
  
  
      @SystemControllerLog(description = "updateScore")
      @RequestMapping(method = RequestMethod.GET)
      @Transactional(isolation = Isolation.REPEATABLE_READ)
      public CustomerBalance updateScore() {
          CustomerBalance customerBalance = repository.findOne(70L);
          System.out.println("start:" + customerBalance.getInvestFreezen());
          customerBalance.setInvestFreezen(customerBalance.getInvestFreezen().subtract(new BigDecimal(5)));
          saveAndFlush(customerBalance);
          System.out.println("end:" + customerBalance.getInvestFreezen());
          return customerBalance;
      }
  
      @Transactional
      public CustomerBalance saveAndFlush(CustomerBalance customerBalance) {
          return repository.saveAndFlush(customerBalance);
      }
  }
  </code></pre>
  
  <p>and the results are</p>
  
  <pre><code>start:-110.00
  end:-115.00
  start:-110.00
  end:-115.00
  start:-115.00
  end:-120.00
  start:-120.00
  end:-125.00
  start:-125.00
  end:-130.00
  start:-130.00
  end:-135.00
  start:-130.00
  end:-135.00
  start:-135.00
  end:-140.00
  start:-140.00
  end:-145.00
  start:-145.00
  end:-150.00
  </code></pre>