FileDocCategorySizeDatePackage
SelectLockingStrategy.javaAPI DocHibernate 3.2.53556Thu Feb 23 10:32:48 GMT 2006org.hibernate.dialect.lock

SelectLockingStrategy

public class SelectLockingStrategy extends Object implements LockingStrategy
A locking strategy where the locks are obtained through select statements.

For non-read locks, this is achieved through the Dialect's specific SELECT ... FOR UPDATE syntax.

see
org.hibernate.dialect.Dialect#getForUpdateString(org.hibernate.LockMode)
see
org.hibernate.dialect.Dialect#appendLockHint(org.hibernate.LockMode, String)
since
3.2
author
Steve Ebersole

Fields Summary
private final org.hibernate.persister.entity.Lockable
lockable
private final org.hibernate.LockMode
lockMode
private final String
sql
Constructors Summary
public SelectLockingStrategy(org.hibernate.persister.entity.Lockable lockable, org.hibernate.LockMode lockMode)
Construct a locking strategy based on SQL SELECT statements.

param
lockable The metadata for the entity to be locked.
param
lockMode Indictates the type of lock to be acquired.

		this.lockable = lockable;
		this.lockMode = lockMode;
		this.sql = generateLockString();
	
Methods Summary
protected java.lang.StringgenerateLockString()

		SessionFactoryImplementor factory = lockable.getFactory();
		SimpleSelect select = new SimpleSelect( factory.getDialect() )
				.setLockMode( lockMode )
				.setTableName( lockable.getRootTableName() )
				.addColumn( lockable.getRootTableIdentifierColumnNames()[0] )
				.addCondition( lockable.getRootTableIdentifierColumnNames(), "=?" );
		if ( lockable.isVersioned() ) {
			select.addCondition( lockable.getVersionColumnName(), "=?" );
		}
		if ( factory.getSettings().isCommentsEnabled() ) {
			select.setComment( lockMode + " lock " + lockable.getEntityName() );
		}
		return select.toStatementString();
	
protected org.hibernate.LockModegetLockMode()

		return lockMode;
	
public voidlock(java.io.Serializable id, java.lang.Object version, java.lang.Object object, org.hibernate.engine.SessionImplementor session)

see
LockingStrategy#lock


		SessionFactoryImplementor factory = session.getFactory();
		try {
			PreparedStatement st = session.getBatcher().prepareSelectStatement( sql );
			try {
				lockable.getIdentifierType().nullSafeSet( st, id, 1, session );
				if ( lockable.isVersioned() ) {
					lockable.getVersionType().nullSafeSet(
							st,
							version,
							lockable.getIdentifierType().getColumnSpan( factory ) + 1,
							session
					);
				}

				ResultSet rs = st.executeQuery();
				try {
					if ( !rs.next() ) {
						if ( factory.getStatistics().isStatisticsEnabled() ) {
							factory.getStatisticsImplementor()
									.optimisticFailure( lockable.getEntityName() );
						}
						throw new StaleObjectStateException( lockable.getEntityName(), id );
					}
				}
				finally {
					rs.close();
				}
			}
			finally {
				session.getBatcher().closeStatement( st );
			}

		}
		catch ( SQLException sqle ) {
			throw JDBCExceptionHelper.convert(
					session.getFactory().getSQLExceptionConverter(),
					sqle,
					"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
					sql
				);
		}