Search in sources :

Example 1 with OptimisticLockException

use of org.hibernate.OptimisticLockException in project hibernate-orm by hibernate.

the class EntityVerifyVersionProcess method doBeforeTransactionCompletion.

@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
    final EntityPersister persister = entry.getPersister();
    if (!entry.isExistsInDatabase()) {
        // HHH-9419: We cannot check for a version of an entry we ourselves deleted
        return;
    }
    final Object latestVersion = persister.getCurrentVersion(entry.getId(), session);
    if (!entry.getVersion().equals(latestVersion)) {
        throw new OptimisticLockException(object, "Newer version [" + latestVersion + "] of entity [" + MessageHelper.infoString(entry.getEntityName(), entry.getId()) + "] found in database");
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) OptimisticLockException(org.hibernate.OptimisticLockException)

Example 2 with OptimisticLockException

use of org.hibernate.OptimisticLockException in project hibernate-orm by hibernate.

the class OptimisticLockingStrategy method lock.

@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
    if (!lockable.isVersioned()) {
        throw new OptimisticLockException(object, "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]");
    }
    final EntityEntry entry = session.getPersistenceContext().getEntry(object);
    // Register the EntityVerifyVersionProcess action to run just prior to transaction commit.
    ((EventSource) session).getActionQueue().registerProcess(new EntityVerifyVersionProcess(object, entry));
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) EntityVerifyVersionProcess(org.hibernate.action.internal.EntityVerifyVersionProcess) OptimisticLockException(org.hibernate.OptimisticLockException)

Aggregations

OptimisticLockException (org.hibernate.OptimisticLockException)2 EntityVerifyVersionProcess (org.hibernate.action.internal.EntityVerifyVersionProcess)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1