Home > OS >  Catch optimistick lock exception hibernate spring boot
Catch optimistick lock exception hibernate spring boot

Time:09-30

i'm trying to intercept an optimistick lock exception and throw another exception but it doesn't work in my case, the exception is catched but i still have error optimistick lock in my console.

//MY DAO

  public Entity getEntitieSimple(...) throws CustomException{
    
        Entity entity="my select";
    }

//MY SERVICE

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public Entity recupererEntity() throws CustomException{
    Entity entity =null;
    try {
         entity =   dao.getEntitieSimple(...);
    }catch (Exception exec){
        throw new CustomException("custom message");
    }
    return entity;
}

@Transactional(propagation = Propagation.REQUIRED)
public void myUpdate() throws HibernateException{
    try {
    // entity update here
    }catch (HibernateException exec){
        log.error("OPTIMISTIC: "  exec.getMessage());
        throw new HibernateException("optimi  update");
    }
}

I'm getting this error evern after catching the exception:

ERROR - 29-09-2022 09:59:44.204 - http-nio-8090-exec-5 - 3933807 -  null - c:96e7d7ca8f7d5637 - org.hibernate.internal.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.apicil.cosy.dop.domain.impl.Entity#1420]]

CodePudding user response:

You have to catch ConcurrencyFailureException

    private void updateStatus(UpdateOrderDto dto, OrdersStatusUpdaterService self) {
        try {
            updateStatusHelper(dto, self);
        } catch (ConcurrencyFailureException ex) {
            log.error("Concurrent modification error. Will try next time. orderId={}", dto.getId(), ex);
        }
    }
  • Related