I am using ORM in Java for connecting to mysql database. In java, I have an API to update a record in db. But when I am getting multiple requests at same time, The record is updated twice with inconsistency. I can not change my entity class to add a version for optimistic lock. How can i maintain persistence without that. The code looks like this:
public void updateRecord(String userId, Double amount) {
User user = userRepository.findById(userId);
double balance = user.getBalance();
user.setBalance(balance-amount);
userRepository.save(user);
}
I am not sure is this where we use pessimist locks or may be @Transactional annotations. Please help. Thanks in adavance !