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();
}
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);
}
}
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);
}
}
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);
}
}
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();
}
Aggregations