Search in sources :

Example 1 with StaleObjectStateException

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

the class PessimisticReadUpdateLockingStrategy 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 2 with StaleObjectStateException

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

the class PessimisticWriteSelectLockingStrategy method lock.

@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
    final String sql = determineSql(timeout);
    final SessionFactoryImplementor factory = session.getFactory();
    try {
        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 e) {
            throw session.getJdbcServices().getSqlExceptionHelper().convert(e, "could not lock: " + MessageHelper.infoString(getLockable(), id, session.getFactory()), sql);
        }
    } catch (JDBCException e) {
        throw new PessimisticEntityLockException(object, "could not obtain pessimistic lock", e);
    }
}
Also used : JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 3 with StaleObjectStateException

use of org.hibernate.StaleObjectStateException 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 4 with StaleObjectStateException

use of org.hibernate.StaleObjectStateException 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 5 with StaleObjectStateException

use of org.hibernate.StaleObjectStateException 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)

Aggregations

StaleObjectStateException (org.hibernate.StaleObjectStateException)13 SQLException (java.sql.SQLException)8 PreparedStatement (java.sql.PreparedStatement)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 HibernateException (org.hibernate.HibernateException)5 JDBCException (org.hibernate.JDBCException)4 ResultSet (java.sql.ResultSet)3 Serializable (java.io.Serializable)2 PersistenceException (javax.persistence.PersistenceException)2 AssertionFailure (org.hibernate.AssertionFailure)2 Session (org.hibernate.Session)2 VersionType (org.hibernate.type.VersionType)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 Transaction (org.hibernate.Transaction)1 WrongClassException (org.hibernate.WrongClassException)1 SQLServerDialect (org.hibernate.dialect.SQLServerDialect)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 EventSource (org.hibernate.event.spi.EventSource)1 SQLGrammarException (org.hibernate.exception.SQLGrammarException)1