title: race conditiion arises in Spring boot restful api using hibernate
body: <p>Here is my controller and api :- </p>
  
  <pre><code>@RequestMapping(value = "/insertData", method = RequestMethod.POST)
  public  ResponseEntity&lt;?&gt; insertTransaction(@Valid @RequestBody Params requestParams,
          BindingResult results) {
      Map&lt;String, String&gt; responseMap = new HashMap&lt;String, String&gt;();
      transactionService.insertTransaction(requestParams);
      return new ResponseEntity&lt;Transactions&gt;(response, HttpStatus.OK);
  }
  </code></pre>
  
  <p>Following is my service class's method having <code>@Transactional</code> annotation on class level. Here I need to insert a row in the table. But before that I'm fetching latest value from db by using this method mentioned in code  [getBalance()]. Now according to this value my table's columns will be updated (last line here in code) <code>dao.insert(Entity)</code></p>
  
  <pre><code>public ResponseDTO insert(ReequestParams request) {
      Entity entity = new entity();
      List&lt;?&gt; list = null;
      request.setCurrTranPrevBal(getBalance(bEntity, loggedInUserId, opponentUserId));
      setCurrentPaymentStatus(request);
  
      BeanUtils.copyProperties(request, entity );
  
      entity .setTransactionId(transactionId());
      entity .setCreatedBy(entity .getInitiatedFrom());
      Date date = DateUtils.getCurrentDateTime();
      entity .setPaymentDate(date);
      entity .setCreatedOn(date);
      entity .setModifiedOn(date);
      entity .setModifiedBy(entity .getInitiatedFrom());
  
  bEntity=balanceDAO.getByBothUsersId(entity .getPayerId(),
        transactionEntity.getPayeeId());
  
  
      } else {
          updatePendingTransactionAmount(entity , bEntity);
  
  
      saveUsersItems(entity );
  
      list = transactionDao.insert(entity );
  
      ResponseDTO dto = copyInResponseDto(list);
      return dto;
  }
  
  private void updatePendingAmount(Entity entity,
          BalanceEntity balanceEntity) {
      int payerId = entity.getPayerId();
      BigDecimal skyCredit = entity.getSkyCredit();
      BigDecimal pendingAmount = bEntity.getPendingAmount();
  
      bEntity.setPendingTransactionAmount(pendingAmount);
      bEntity.setModifiedOn(DateUtils.getCurrentDateTime());
      bEntity.setModifiedBy(transactionEntity.getInitiatedFrom());
      bEntity.setTransacted(true);
  
  }
  </code></pre>
  
  <p>But when multiple http requests hits the api (load testing) , then insertion is done before reading the latest value. What I want is transaction must be inserted after reading the latest value. Insertion must be done one by one.
  I tried synchronized block too and also isolatio levels but not working.
  Here we are updating two tables by <code>usersBalanceDAO</code> and <code>transactionDao</code>.
  Please help me implementing lock or synchronization if neeed here or is there any architectural issue .</p>