Search in sources :

Example 36 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class PessimisticWriteSelectLockingStrategy method generateLockString.

protected String generateLockString(int lockTimeout) {
    final SessionFactoryImplementor factory = getLockable().getFactory();
    final LockOptions lockOptions = new LockOptions(getLockMode());
    lockOptions.setTimeOut(lockTimeout);
    final SimpleSelect select = new SimpleSelect(factory.getDialect()).setLockOptions(lockOptions).setTableName(getLockable().getRootTableName()).addColumn(getLockable().getRootTableIdentifierColumnNames()[0]).addCondition(getLockable().getRootTableIdentifierColumnNames(), "=?");
    if (getLockable().isVersioned()) {
        select.addCondition(getLockable().getVersionColumnName(), "=?");
    }
    if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment(getLockMode() + " lock " + getLockable().getEntityName());
    }
    return select.toStatementString();
}
Also used : LockOptions(org.hibernate.LockOptions) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SimpleSelect(org.hibernate.sql.SimpleSelect)

Example 37 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class PessimisticWriteUpdateLockingStrategy method lock.

@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
    if (!lockable.isVersioned()) {
        throw new HibernateException("write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]");
    }
    final SessionFactoryImplementor factory = session.getFactory();
    try {
        try {
            final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
            try {
                lockable.getVersionType().nullSafeSet(st, version, 1, session);
                int offset = 2;
                lockable.getIdentifierType().nullSafeSet(st, id, offset, session);
                offset += lockable.getIdentifierType().getColumnSpan(factory);
                if (lockable.isVersioned()) {
                    lockable.getVersionType().nullSafeSet(st, version, offset, session);
                }
                final int affected = session.getJdbcCoordinator().getResultSetReturn().executeUpdate(st);
                // todo:  should this instead check for exactly one row modified?
                if (affected < 0) {
                    if (factory.getStatistics().isStatisticsEnabled()) {
                        factory.getStatistics().optimisticFailure(lockable.getEntityName());
                    }
                    throw new StaleObjectStateException(lockable.getEntityName(), id);
                }
            } finally {
                session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
                session.getJdbcCoordinator().afterStatementExecution();
            }
        } catch (SQLException e) {
            throw session.getJdbcServices().getSqlExceptionHelper().convert(e, "could not lock: " + MessageHelper.infoString(lockable, id, session.getFactory()), sql);
        }
    } catch (JDBCException e) {
        throw new PessimisticEntityLockException(object, "could not obtain pessimistic lock", e);
    }
}
Also used : JDBCException(org.hibernate.JDBCException) HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PreparedStatement(java.sql.PreparedStatement) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 38 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class SelectLockingStrategy method lock.

@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) throws StaleObjectStateException, JDBCException {
    final String sql = determineSql(timeout);
    final SessionFactoryImplementor factory = session.getFactory();
    try {
        final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
        try {
            getLockable().getIdentifierType().nullSafeSet(st, id, 1, session);
            if (getLockable().isVersioned()) {
                getLockable().getVersionType().nullSafeSet(st, version, getLockable().getIdentifierType().getColumnSpan(factory) + 1, session);
            }
            final ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract(st);
            try {
                if (!rs.next()) {
                    if (factory.getStatistics().isStatisticsEnabled()) {
                        factory.getStatistics().optimisticFailure(getLockable().getEntityName());
                    }
                    throw new StaleObjectStateException(getLockable().getEntityName(), id);
                }
            } finally {
                session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(rs, st);
            }
        } finally {
            session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
            session.getJdbcCoordinator().afterStatementExecution();
        }
    } catch (SQLException sqle) {
        throw session.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not lock: " + MessageHelper.infoString(getLockable(), id, session.getFactory()), sql);
    }
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 39 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class UpdateLockingStrategy method lock.

@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) throws StaleObjectStateException, JDBCException {
    if (!lockable.isVersioned()) {
        throw new HibernateException("write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]");
    }
    // todo : should we additionally check the current isolation mode explicitly?
    final SessionFactoryImplementor factory = session.getFactory();
    try {
        final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
        try {
            lockable.getVersionType().nullSafeSet(st, version, 1, session);
            int offset = 2;
            lockable.getIdentifierType().nullSafeSet(st, id, offset, session);
            offset += lockable.getIdentifierType().getColumnSpan(factory);
            if (lockable.isVersioned()) {
                lockable.getVersionType().nullSafeSet(st, version, offset, session);
            }
            final int affected = session.getJdbcCoordinator().getResultSetReturn().executeUpdate(st);
            if (affected < 0) {
                if (factory.getStatistics().isStatisticsEnabled()) {
                    factory.getStatistics().optimisticFailure(lockable.getEntityName());
                }
                throw new StaleObjectStateException(lockable.getEntityName(), id);
            }
        } finally {
            session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
            session.getJdbcCoordinator().afterStatementExecution();
        }
    } catch (SQLException sqle) {
        throw session.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not lock: " + MessageHelper.infoString(lockable, id, session.getFactory()), sql);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PreparedStatement(java.sql.PreparedStatement) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 40 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class UpdateLockingStrategy method generateLockString.

protected String generateLockString() {
    final SessionFactoryImplementor factory = lockable.getFactory();
    final Update update = new Update(factory.getDialect());
    update.setTableName(lockable.getRootTableName());
    update.addPrimaryKeyColumns(lockable.getRootTableIdentifierColumnNames());
    update.setVersionColumnName(lockable.getVersionColumnName());
    update.addColumn(lockable.getVersionColumnName());
    if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
        update.setComment(lockMode + " lock " + lockable.getEntityName());
    }
    return update.toStatementString();
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Update(org.hibernate.sql.Update)

Aggregations

SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)129 Test (org.junit.Test)46 Configuration (org.hibernate.cfg.Configuration)27 SQLException (java.sql.SQLException)18 Type (org.hibernate.type.Type)16 EntityPersister (org.hibernate.persister.entity.EntityPersister)13 Connection (java.sql.Connection)12 HibernateException (org.hibernate.HibernateException)12 PreparedStatement (java.sql.PreparedStatement)11 Metadata (org.hibernate.boot.Metadata)11 Statement (java.sql.Statement)10 ArrayList (java.util.ArrayList)10 JdbcConnectionAccess (org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess)10 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)10 Serializable (java.io.Serializable)9 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)8 HashMap (java.util.HashMap)7 Integrator (org.hibernate.integrator.spi.Integrator)7 SessionFactoryServiceRegistry (org.hibernate.service.spi.SessionFactoryServiceRegistry)7 Map (java.util.Map)6