Search in sources :

Example 1 with PreparedStatementAdaptor.bind

use of org.hibernate.reactive.adaptor.impl.PreparedStatementAdaptor.bind in project hibernate-reactive by hibernate.

the class ReactiveAbstractEntityPersister method lockReactive.

@Override
default CompletionStage<Void> lockReactive(Serializable id, Object version, Object object, LockOptions lockOptions, SharedSessionContractImplementor session) throws HibernateException {
    LockMode lockMode = lockOptions.getLockMode();
    Object nextVersion = nextVersionForLock(lockMode, id, version, object, session);
    String sql;
    boolean writeLock;
    switch(lockMode) {
        // 0) noop
        case NONE:
            return voidFuture();
        // 1) select ... for share
        case PESSIMISTIC_READ:
        // 2) select ... for update
        case PESSIMISTIC_WRITE:
        case UPGRADE:
        // 3) select ... for nowait
        case UPGRADE_NOWAIT:
        // 4) select ... for update skip locked
        case UPGRADE_SKIPLOCKED:
            // TODO: introduce separate support for PESSIMISTIC_READ
            // the current implementation puts the version number in
            // the where clause and the id in the select list, whereas
            // it would be better to actually select and check the
            // version number (same problem in hibernate-core)
            sql = generateSelectLockString(lockOptions);
            writeLock = false;
            break;
        // 5) update ... set version
        case PESSIMISTIC_FORCE_INCREMENT:
        case FORCE:
            sql = generateUpdateLockString(lockOptions);
            writeLock = true;
            break;
        // locks obtained in the before completion phase
        case OPTIMISTIC:
        case OPTIMISTIC_FORCE_INCREMENT:
            throw new AssertionFailure("optimistic lock mode is not supported here");
        // other operations
        case READ:
        case WRITE:
            throw new AssertionFailure("implicit lock mode is not supported here");
        default:
            throw new AssertionFailure("illegal lock mode");
    }
    Object[] arguments = PreparedStatementAdaptor.bind(statement -> {
        int offset = 1;
        if (writeLock) {
            getVersionType().nullSafeSet(statement, nextVersion, offset, session);
            offset++;
        }
        getIdentifierType().nullSafeSet(statement, id, offset, session);
        offset += getIdentifierType().getColumnSpan(getFactory());
        if (isVersioned()) {
            getVersionType().nullSafeSet(statement, version, offset, session);
        }
    });
    ReactiveConnection connection = getReactiveConnection(session);
    CompletionStage<Boolean> lock = writeLock ? connection.update(sql, arguments).thenApply(affected -> affected > 0) : connection.select(sql, arguments).thenApply(Iterator::hasNext);
    return lock.thenAccept(rowExisted -> {
        if (!rowExisted) {
            throw new StaleObjectStateException(getEntityName(), id);
        }
    }).handle((r, e) -> {
        logSqlException(e, () -> "could not lock: " + infoString(this, id, getFactory()), sql);
        return returnOrRethrow(e, r);
    });
}
Also used : Arrays(java.util.Arrays) CompletionStages.completedFuture(org.hibernate.reactive.util.impl.CompletionStages.completedFuture) ReactiveDynamicBatchingEntityLoaderBuilder(org.hibernate.reactive.loader.entity.impl.ReactiveDynamicBatchingEntityLoaderBuilder) Expectations.appropriateExpectation(org.hibernate.jdbc.Expectations.appropriateExpectation) EventSource(org.hibernate.event.spi.EventSource) Log(org.hibernate.reactive.logging.impl.Log) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) LoggerFactory(org.hibernate.reactive.logging.impl.LoggerFactory) ReactiveConnectionSupplier(org.hibernate.reactive.session.ReactiveConnectionSupplier) NonIdentifierAttribute(org.hibernate.tuple.NonIdentifierAttribute) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) JDBCException(org.hibernate.JDBCException) PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) ResultSet(java.sql.ResultSet) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) CompletionStages.voidFuture(org.hibernate.reactive.util.impl.CompletionStages.voidFuture) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Mutiny(org.hibernate.reactive.mutiny.Mutiny) LockOptions(org.hibernate.LockOptions) Attribute(javax.persistence.metamodel.Attribute) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) MethodHandles(java.lang.invoke.MethodHandles) StaleObjectStateException(org.hibernate.StaleObjectStateException) LazyAttributeDescriptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor) Set(java.util.Set) ReactiveEntityLoader(org.hibernate.reactive.loader.entity.impl.ReactiveEntityLoader) MutinyValueGenerator(org.hibernate.reactive.tuple.MutinyValueGenerator) PreparedStatement(java.sql.PreparedStatement) Serializable(java.io.Serializable) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) ArrayHelper(org.hibernate.internal.util.collections.ArrayHelper) Lockable(org.hibernate.persister.entity.Lockable) MessageHelper.infoString(org.hibernate.pretty.MessageHelper.infoString) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Expectation(org.hibernate.jdbc.Expectation) Dialect(org.hibernate.dialect.Dialect) CompletionStages.loop(org.hibernate.reactive.util.impl.CompletionStages.loop) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Update(org.hibernate.sql.Update) HibernateException(org.hibernate.HibernateException) PreparedStatementAdaptor(org.hibernate.reactive.adaptor.impl.PreparedStatementAdaptor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) AbstractEntityPersister.determineValueNullness(org.hibernate.persister.entity.AbstractEntityPersister.determineValueNullness) OptimisticLockStyle(org.hibernate.engine.OptimisticLockStyle) AssertionFailure(org.hibernate.AssertionFailure) CompletionStages.logSqlException(org.hibernate.reactive.util.impl.CompletionStages.logSqlException) Session(org.hibernate.Session) StageSessionFactoryImpl(org.hibernate.reactive.stage.impl.StageSessionFactoryImpl) InMemoryValueGenerationStrategy(org.hibernate.tuple.InMemoryValueGenerationStrategy) EntityType(org.hibernate.type.EntityType) UniqueEntityLoader(org.hibernate.loader.entity.UniqueEntityLoader) MultiLoadOptions(org.hibernate.persister.entity.MultiLoadOptions) SQLException(java.sql.SQLException) PreparedStatementAdaptor.bind(org.hibernate.reactive.adaptor.impl.PreparedStatementAdaptor.bind) StageValueGenerator(org.hibernate.reactive.tuple.StageValueGenerator) ArrayHelper.trim(org.hibernate.internal.util.collections.ArrayHelper.trim) StageSessionImpl(org.hibernate.reactive.stage.impl.StageSessionImpl) ReactiveSession(org.hibernate.reactive.session.ReactiveSession) ArrayHelper.join(org.hibernate.internal.util.collections.ArrayHelper.join) MutinySessionFactoryImpl(org.hibernate.reactive.mutiny.impl.MutinySessionFactoryImpl) LockMode(org.hibernate.LockMode) Iterator(java.util.Iterator) Parameters(org.hibernate.reactive.pool.impl.Parameters) Versioning(org.hibernate.engine.internal.Versioning) SimpleSelect(org.hibernate.sql.SimpleSelect) MutinySessionImpl(org.hibernate.reactive.mutiny.impl.MutinySessionImpl) ValueGenerator(org.hibernate.tuple.ValueGenerator) CompletionStages.returnOrRethrow(org.hibernate.reactive.util.impl.CompletionStages.returnOrRethrow) EntityKey(org.hibernate.engine.spi.EntityKey) Delete(org.hibernate.sql.Delete) IdentifierGeneration.castToIdentifierType(org.hibernate.reactive.id.impl.IdentifierGeneration.castToIdentifierType) AbstractEntityPersister.isValueGenerationRequired(org.hibernate.persister.entity.AbstractEntityPersister.isValueGenerationRequired) Stage(org.hibernate.reactive.stage.Stage) GenerationTiming(org.hibernate.tuple.GenerationTiming) VERSION_COLUMN_ALIAS(org.hibernate.persister.entity.AbstractEntityPersister.VERSION_COLUMN_ALIAS) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) VersionType(org.hibernate.type.VersionType) EntityEntry(org.hibernate.engine.spi.EntityEntry) Type(org.hibernate.type.Type) ReactiveConnection(org.hibernate.reactive.pool.ReactiveConnection) AssertionFailure(org.hibernate.AssertionFailure) ReactiveConnection(org.hibernate.reactive.pool.ReactiveConnection) LockMode(org.hibernate.LockMode) MessageHelper.infoString(org.hibernate.pretty.MessageHelper.infoString) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Aggregations

Serializable (java.io.Serializable)1 MethodHandles (java.lang.invoke.MethodHandles)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Set (java.util.Set)1 CompletionStage (java.util.concurrent.CompletionStage)1 Attribute (javax.persistence.metamodel.Attribute)1 AssertionFailure (org.hibernate.AssertionFailure)1 HibernateException (org.hibernate.HibernateException)1 JDBCException (org.hibernate.JDBCException)1 LockMode (org.hibernate.LockMode)1 LockOptions (org.hibernate.LockOptions)1 Session (org.hibernate.Session)1 StaleObjectStateException (org.hibernate.StaleObjectStateException)1 LazyAttributeDescriptor (org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor)1 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)1