Java Code Examples for org.hibernate.LockOptions#setTimeOut()

The following examples show how to use org.hibernate.LockOptions#setTimeOut() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CascadingActions.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void cascade(
		EventSource session,
		Object child,
		String entityName,
		Object anything,
		boolean isCascadeDeleteEnabled) {
	LOG.tracev( "Cascading to lock: {0}", entityName );
	LockMode lockMode = LockMode.NONE;
	LockOptions lr = new LockOptions();
	if ( anything instanceof LockOptions ) {
		LockOptions lockOptions = (LockOptions) anything;
		lr.setTimeOut( lockOptions.getTimeOut() );
		lr.setScope( lockOptions.getScope() );
		lr.setFollowOnLocking( lockOptions.getFollowOnLocking() );
		if ( lockOptions.getScope() ) {
			lockMode = lockOptions.getLockMode();
		}
	}
	lr.setLockMode( lockMode );
	session.buildLockRequest( lr ).lock( entityName, child );
}
 
Example 2
Source File: PessimisticWriteSelectLockingStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 3
Source File: SelectLockingStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected String generateLockString(int timeout) {
	final SessionFactoryImplementor factory = getLockable().getFactory();
	final LockOptions lockOptions = new LockOptions( getLockMode() );
	lockOptions.setTimeOut( timeout );
	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();
}
 
Example 4
Source File: PessimisticReadSelectLockingStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 5
Source File: QueryHintDefinition.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private LockOptions determineLockOptions(LockModeType lockModeType, Integer lockTimeoutHint, Boolean followOnLocking) {

		LockOptions lockOptions = new LockOptions( LockModeConverter.convertToLockMode( lockModeType ) )
				.setFollowOnLocking( followOnLocking );
		if ( lockTimeoutHint != null ) {
			lockOptions.setTimeOut( lockTimeoutHint );
		}

		return lockOptions;
	}
 
Example 6
Source File: Loader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected boolean shouldUseFollowOnLocking(
		QueryParameters parameters,
		Dialect dialect,
		List<AfterLoadAction> afterLoadActions) {
	if ( ( parameters.getLockOptions().getFollowOnLocking() == null && dialect.useFollowOnLocking( parameters ) ) ||
			( parameters.getLockOptions().getFollowOnLocking() != null && parameters.getLockOptions().getFollowOnLocking() ) ) {
		// currently only one lock mode is allowed in follow-on locking
		final LockMode lockMode = determineFollowOnLockMode( parameters.getLockOptions() );
		final LockOptions lockOptions = new LockOptions( lockMode );
		if ( lockOptions.getLockMode() != LockMode.UPGRADE_SKIPLOCKED ) {
			if ( lockOptions.getLockMode() != LockMode.NONE ) {
				LOG.usingFollowOnLocking();
			}
			lockOptions.setTimeOut( parameters.getLockOptions().getTimeOut() );
			lockOptions.setScope( parameters.getLockOptions().getScope() );
			afterLoadActions.add(
					new AfterLoadAction() {
						@Override
						public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
							( (Session) session ).buildLockRequest( lockOptions ).lock(
									persister.getEntityName(),
									entity
							);
						}
					}
			);
			parameters.setLockOptions( new LockOptions() );
			return true;
		}
	}
	return false;
}
 
Example 7
Source File: QueryLoader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected String applyLocks(
		String sql,
		QueryParameters parameters,
		Dialect dialect,
		List<AfterLoadAction> afterLoadActions) throws QueryException {
	// can't cache this stuff either (per-invocation)
	// we are given a map of user-alias -> lock mode
	// create a new map of sql-alias -> lock mode

	final LockOptions lockOptions = parameters.getLockOptions();

	if ( lockOptions == null ||
			( lockOptions.getLockMode() == LockMode.NONE && lockOptions.getAliasLockCount() == 0 ) ) {
		return sql;
	}


	// user is request locking, lets see if we can apply locking directly to the SQL...

	// 		some dialects wont allow locking with paging...
	if ( shouldUseFollowOnLocking( parameters, dialect, afterLoadActions ) ) {
		return sql;
	}

	//		there are other conditions we might want to add here, such as checking the result types etc
	//		but those are better served after we have redone the SQL generation to use ASTs.


	// we need both the set of locks and the columns to reference in locks
	// as the ultimate output of this section...
	final LockOptions locks = new LockOptions( lockOptions.getLockMode() );
	final Map<String, String[]> keyColumnNames = dialect.forUpdateOfColumns()
			? new HashMap<>()
			: null;

	locks.setScope( lockOptions.getScope() );
	locks.setTimeOut( lockOptions.getTimeOut() );

	for ( Map.Entry<String, String> entry : sqlAliasByEntityAlias.entrySet() ) {
		final String userAlias = entry.getKey();
		final String drivingSqlAlias = entry.getValue();
		if ( drivingSqlAlias == null ) {
			throw new IllegalArgumentException( "could not locate alias to apply lock mode : " + userAlias );
		}
		// at this point we have (drivingSqlAlias) the SQL alias of the driving table
		// corresponding to the given user alias.  However, the driving table is not
		// (necessarily) the table against which we want to apply locks.  Mainly,
		// the exception case here is joined-subclass hierarchies where we instead
		// want to apply the lock against the root table (for all other strategies,
		// it just happens that driving and root are the same).
		final QueryNode select = (QueryNode) queryTranslator.getSqlAST();
		final Lockable drivingPersister = (Lockable) select.getFromClause()
				.findFromElementByUserOrSqlAlias( userAlias, drivingSqlAlias )
				.getQueryable();
		final String sqlAlias = drivingPersister.getRootTableAlias( drivingSqlAlias );

		final LockMode effectiveLockMode = lockOptions.getEffectiveLockMode( userAlias );
		locks.setAliasSpecificLockMode( sqlAlias, effectiveLockMode );

		if ( keyColumnNames != null ) {
			keyColumnNames.put( sqlAlias, drivingPersister.getRootTableIdentifierColumnNames() );
		}
	}

	// apply the collected locks and columns
	return dialect.applyLocksToSql( sql, locks, keyColumnNames );
}
 
Example 8
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void setLockOptions(Map<String, Object> props, LockOptions options) {
	Object lockScope = props.get( JPA_LOCK_SCOPE );
	if ( lockScope instanceof String && PessimisticLockScope.valueOf( ( String ) lockScope ) == PessimisticLockScope.EXTENDED ) {
		options.setScope( true );
	}
	else if ( lockScope instanceof PessimisticLockScope ) {
		boolean extended = PessimisticLockScope.EXTENDED.equals( lockScope );
		options.setScope( extended );
	}
	else if ( lockScope != null ) {
		throw new PersistenceException( "Unable to parse " + JPA_LOCK_SCOPE + ": " + lockScope );
	}

	Object lockTimeout = props.get( JPA_LOCK_TIMEOUT );
	int timeout = 0;
	boolean timeoutSet = false;
	if ( lockTimeout instanceof String ) {
		timeout = Integer.parseInt( ( String ) lockTimeout );
		timeoutSet = true;
	}
	else if ( lockTimeout instanceof Number ) {
		timeout = ( (Number) lockTimeout ).intValue();
		timeoutSet = true;
	}
	else if ( lockTimeout != null ) {
		throw new PersistenceException( "Unable to parse " + JPA_LOCK_TIMEOUT + ": " + lockTimeout );
	}

	if ( timeoutSet ) {
		if ( timeout == LockOptions.SKIP_LOCKED ) {
			options.setTimeOut( LockOptions.SKIP_LOCKED );
		}
		else if ( timeout < 0 ) {
			options.setTimeOut( LockOptions.WAIT_FOREVER );
		}
		else if ( timeout == 0 ) {
			options.setTimeOut( LockOptions.NO_WAIT );
		}
		else {
			options.setTimeOut( timeout );
		}
	}
}