Search in sources :

Example 1 with ReactiveEntityIncrementVersionProcess

use of org.hibernate.reactive.engine.impl.ReactiveEntityIncrementVersionProcess in project hibernate-reactive by hibernate.

the class DefaultReactiveLockEventListener method upgradeLock.

/**
 * Performs a pessimistic lock upgrade on a given entity, if needed.
 *
 * @param object The entity for which to upgrade the lock.
 * @param entry The entity's EntityEntry instance.
 * @param lockOptions contains the requested lock mode.
 * @param source The session which is the source of the event being processed.
 */
protected CompletionStage<Void> upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource source) {
    LockMode requestedLockMode = lockOptions.getLockMode();
    if (requestedLockMode.greaterThan(entry.getLockMode())) {
        if (entry.getStatus() != Status.MANAGED) {
            throw new ObjectDeletedException("attempted to lock a deleted instance", entry.getId(), entry.getPersister().getEntityName());
        }
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Locking {0} in mode: {1}", infoString(entry.getPersister(), entry.getId(), source.getFactory()), requestedLockMode);
        }
        switch(requestedLockMode) {
            case OPTIMISTIC:
                ((ReactiveSession) source).getReactiveActionQueue().registerProcess(new ReactiveEntityVerifyVersionProcess(object));
                entry.setLockMode(requestedLockMode);
                return voidFuture();
            case OPTIMISTIC_FORCE_INCREMENT:
                ((ReactiveSession) source).getReactiveActionQueue().registerProcess(new ReactiveEntityIncrementVersionProcess(object));
                entry.setLockMode(requestedLockMode);
                return voidFuture();
            default:
                return doUpgradeLock(object, entry, lockOptions, source);
        }
    } else {
        return voidFuture();
    }
}
Also used : ReactiveEntityVerifyVersionProcess(org.hibernate.reactive.engine.impl.ReactiveEntityVerifyVersionProcess) ObjectDeletedException(org.hibernate.ObjectDeletedException) ReactiveEntityIncrementVersionProcess(org.hibernate.reactive.engine.impl.ReactiveEntityIncrementVersionProcess) LockMode(org.hibernate.LockMode)

Example 2 with ReactiveEntityIncrementVersionProcess

use of org.hibernate.reactive.engine.impl.ReactiveEntityIncrementVersionProcess in project hibernate-reactive by hibernate.

the class DefaultReactivePostLoadEventListener method onPostLoad.

@Override
public void onPostLoad(PostLoadEvent event) {
    final Object entity = event.getEntity();
    callbackRegistry.postLoad(entity);
    final SessionImplementor session = event.getSession();
    final EntityEntry entry = session.getPersistenceContextInternal().getEntry(entity);
    if (entry == null) {
        throw new AssertionFailure("possible non-threadsafe access to the session");
    }
    LockMode lockMode = entry.getLockMode();
    if (LockMode.OPTIMISTIC_FORCE_INCREMENT.equals(lockMode)) {
        ((ReactiveSession) session).getReactiveActionQueue().registerProcess(new ReactiveEntityIncrementVersionProcess(entity));
    } else if (LockMode.OPTIMISTIC.equals(lockMode)) {
        ((ReactiveSession) session).getReactiveActionQueue().registerProcess(new ReactiveEntityVerifyVersionProcess(entity));
    }
}
Also used : ReactiveSession(org.hibernate.reactive.session.ReactiveSession) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) ReactiveEntityVerifyVersionProcess(org.hibernate.reactive.engine.impl.ReactiveEntityVerifyVersionProcess) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ReactiveEntityIncrementVersionProcess(org.hibernate.reactive.engine.impl.ReactiveEntityIncrementVersionProcess) LockMode(org.hibernate.LockMode)

Aggregations

LockMode (org.hibernate.LockMode)2 ReactiveEntityIncrementVersionProcess (org.hibernate.reactive.engine.impl.ReactiveEntityIncrementVersionProcess)2 ReactiveEntityVerifyVersionProcess (org.hibernate.reactive.engine.impl.ReactiveEntityVerifyVersionProcess)2 AssertionFailure (org.hibernate.AssertionFailure)1 ObjectDeletedException (org.hibernate.ObjectDeletedException)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 ReactiveSession (org.hibernate.reactive.session.ReactiveSession)1