Optimistic Lock

0

First method is working fine but others has error. Why am i getting this error. 2 method have same purpose, one of them returns entity, others not.

**Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.x.y.z.entity.Customer#6542]

Object of class [com.x.y.z.entity.Customer#6542] optimistic locking failed nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction. **

@Override
@CacheEvict(cacheNames = CacheConstants.CUSTOMER_CACHE, key = "#customer.token + #customer.customerToken", cacheManager = "redisCacheManager")
public Customer updateCustomer(Customer customer) {
    return repository.save(customer); // NO ERROR
}

@Override
public void updateCustomerStatus(Customer customer, TokenStatus status) {
    customer.setStatus(status);
    repository.save(customer); // ERROR (OPT. LOCK)
}

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateCustomerStatus(Customer customer, TokenStatus status) {
    customer.setStatus(status);
    repository.save(customer); // ERROR (OPT. LOCK)
}

@Override
public void updateCustomerStatus(Customer customer, TokenStatus status) {
    customer.setStatus(status);
    repository.saveAndFlush(customer); // ERROR (OPT. LOCK)
}

With this little information is going to be very hard for anyone to help you. You are apparently using some other framework’s methods (repository#save()), which are not exposed by Hibernate. We also have no context of what the Customer entity looks like or what the status of the instances passed to this method is in the first place. Also, what version of Hibernate are you using? Please provide more details.

1 Like