0

I'm writing a regular JEE8 application with JPA with @Version (OptimisticLock). My DAO is a CDI bean marked with @Transactional(value = TxType.REQUIRED)

when an update is conflicting: org.hibernate.StaleObjectStateException should be wrapped in javax.persistence.OptimisticLockException but that's not the case.

I receive a javax.transaction.RollbackException and the cause is org.hibernate.StaleObjectStateException.

How such thing is possible ?

Were is the expected javax.persistence.OptimisticLockException ? It appears nowhere (in all exception causes chain).

tested on Wildfly 10 and 14: hibernate 5.0.10 and 5.3.6

What I'm doing is updating an Actor with a child table ActorProperties. Actor is the one with @Version with TimeStamp. I do something like that:

public Supplier<Long> updateActorOptimiticLock(Actor actor) {
    Map<String,Object> hints = new HashMap<String,Object>();
    hints.put("javax.persistence.fetchgraph", em.getEntityGraph("actor with accesses and their props"));
    ActorEntity currentEntity = em.find(ActorEntity.class, actor.getId(),hints);
    if (currentEntity.getLastUpdate().getTime()!=actor.getLastUpdate())
    {
        throw new OptimisticLockException("Dto obsolete");
    }
    ActorEntity detachedActorEntity = mt.fromDto(actor);
    em.merge(detachedActorEntity);
    em.lock(currentEntity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);

    return ()->currentEntity.getLastUpdate().getTime();
}

I force the increment because properties don't have @Version in my model.

Also My DAO looks like this:

@Transactional(value = TxType.REQUIRED)
public class ServiceDAO {
    @PersistenceContext
    protected EntityManager em;
}
  • `StaleObjectStateException should be wrapped in OptimisticLockException` where did you read that? Does Hibernate documentation is mentioning this? Both are different exceptions as [per docs](https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/HibernateException.html) – nortontgueno Mar 04 '19 at 20:02
  • You should post your dao and entity class... – Carlitos Way Mar 04 '19 at 22:54
  • @ngueno this is defined by the JPA specification: "The OptimisticLockException is thrown by the persistence provider when an optimistic locking conflict occurs. This exception may be thrown as part of an API call, at flush, or at commit time. The current transaction, if one is active, will be marked for rollback." – Mike Dudley Mar 05 '19 at 08:30
  • @Mike this is not mention any kind of wrapping within another exception, just describes the behavior to be handled in case of optimistic lock. I believe you are mixing two different concepts – nortontgueno Mar 05 '19 at 14:50

0 Answers0