Java Code Examples for org.springframework.transaction.TransactionDefinition#getPropagationBehavior()

The following examples show how to use org.springframework.transaction.TransactionDefinition#getPropagationBehavior() . 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: Neo4jTransactionUtils.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
/**
 * Maps a Spring {@link TransactionDefinition transaction definition} to a native Neo4j driver transaction.
 * Only the default isolation leven ({@link TransactionDefinition#ISOLATION_DEFAULT}) and
 * {@link TransactionDefinition#PROPAGATION_REQUIRED propagation required} behaviour are supported.
 *
 * @param definition The transaction definition passed to a Neo4j transaction manager
 * @return A Neo4j native transaction configuration
 */
static TransactionConfig createTransactionConfigFrom(TransactionDefinition definition) {

	if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
		throw new InvalidIsolationLevelException(
			"Neo4jTransactionManager is not allowed to support custom isolation levels.");
	}

	int propagationBehavior = definition.getPropagationBehavior();
	if (!(propagationBehavior == TransactionDefinition.PROPAGATION_REQUIRED || propagationBehavior == TransactionDefinition.PROPAGATION_REQUIRES_NEW)) {
		throw new IllegalTransactionStateException("Neo4jTransactionManager only supports 'required' or 'requires new' propagation.");
	}

	TransactionConfig.Builder builder = TransactionConfig.builder();
	if (definition.getTimeout() > 0) {
		builder = builder.withTimeout(Duration.ofSeconds(definition.getTimeout()));
	}

	return builder.build();
}
 
Example 2
Source File: DatastoreTransactionManager.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
protected void doBegin(Object transactionObject,
		TransactionDefinition transactionDefinition) throws TransactionException {
	if (transactionDefinition
			.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT
			&& transactionDefinition
					.getIsolationLevel() != TransactionDefinition.ISOLATION_SERIALIZABLE) {
		throw new IllegalStateException(
				"DatastoreTransactionManager supports only isolation level "
						+ "TransactionDefinition.ISOLATION_DEFAULT or ISOLATION_SERIALIZABLE");
	}
	if (transactionDefinition
			.getPropagationBehavior() != TransactionDefinition.PROPAGATION_REQUIRED) {
		throw new IllegalStateException(
				"DatastoreTransactionManager supports only propagation behavior "
						+ "TransactionDefinition.PROPAGATION_REQUIRED");
	}
	Tx tx = (Tx) transactionObject;
	if (transactionDefinition.isReadOnly()) {
		tx.transaction = tx.datastore.newTransaction(READ_ONLY_OPTIONS);
	}
	else {
		tx.transaction = tx.datastore.newTransaction();
	}

	TransactionSynchronizationManager.bindResource(tx.datastore, tx);
}
 
Example 3
Source File: WebSphereUowTransactionManager.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Nullable
public <T> T execute(@Nullable TransactionDefinition definition, TransactionCallback<T> callback)
		throws TransactionException {

	// Use defaults if no transaction definition given.
	TransactionDefinition def = (definition != null ? definition : TransactionDefinition.withDefaults());

	if (def.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
		throw new InvalidTimeoutException("Invalid transaction timeout", def.getTimeout());
	}

	UOWManager uowManager = obtainUOWManager();
	int pb = def.getPropagationBehavior();
	boolean existingTx = (uowManager.getUOWStatus() != UOWSynchronizationRegistry.UOW_STATUS_NONE &&
			uowManager.getUOWType() != UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION);

	int uowType = UOWSynchronizationRegistry.UOW_TYPE_GLOBAL_TRANSACTION;
	boolean joinTx = false;
	boolean newSynch = false;

	if (existingTx) {
		if (pb == TransactionDefinition.PROPAGATION_NEVER) {
			throw new IllegalTransactionStateException(
					"Transaction propagation 'never' but existing transaction found");
		}
		if (pb == TransactionDefinition.PROPAGATION_NESTED) {
			throw new NestedTransactionNotSupportedException(
					"Transaction propagation 'nested' not supported for WebSphere UOW transactions");
		}
		if (pb == TransactionDefinition.PROPAGATION_SUPPORTS ||
				pb == TransactionDefinition.PROPAGATION_REQUIRED ||
				pb == TransactionDefinition.PROPAGATION_MANDATORY) {
			joinTx = true;
			newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
		}
		else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION;
			newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
		}
		else {
			newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
		}
	}
	else {
		if (pb == TransactionDefinition.PROPAGATION_MANDATORY) {
			throw new IllegalTransactionStateException(
					"Transaction propagation 'mandatory' but no existing transaction found");
		}
		if (pb == TransactionDefinition.PROPAGATION_SUPPORTS ||
				pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED ||
				pb == TransactionDefinition.PROPAGATION_NEVER) {
			uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION;
			newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
		}
		else {
			newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
		}
	}

	boolean debug = logger.isDebugEnabled();
	if (debug) {
		logger.debug("Creating new transaction with name [" + def.getName() + "]: " + def);
	}
	SuspendedResourcesHolder suspendedResources = (!joinTx ? suspend(null) : null);
	UOWActionAdapter<T> action = null;
	try {
		if (def.getTimeout() > TransactionDefinition.TIMEOUT_DEFAULT) {
			uowManager.setUOWTimeout(uowType, def.getTimeout());
		}
		if (debug) {
			logger.debug("Invoking WebSphere UOW action: type=" + uowType + ", join=" + joinTx);
		}
		action = new UOWActionAdapter<>(
				def, callback, (uowType == UOWManager.UOW_TYPE_GLOBAL_TRANSACTION), !joinTx, newSynch, debug);
		uowManager.runUnderUOW(uowType, joinTx, action);
		if (debug) {
			logger.debug("Returned from WebSphere UOW action: type=" + uowType + ", join=" + joinTx);
		}
		return action.getResult();
	}
	catch (UOWException | UOWActionException ex) {
		TransactionSystemException tse =
				new TransactionSystemException("UOWManager transaction processing failed", ex);
		Throwable appEx = action.getException();
		if (appEx != null) {
			logger.error("Application exception overridden by rollback exception", appEx);
			tse.initApplicationException(appEx);
		}
		throw tse;
	}
	finally {
		if (suspendedResources != null) {
			resume(null, suspendedResources);
		}
	}
}
 
Example 4
Source File: DefaultTransactionDefinition.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Copy constructor. Definition can be modified through bean property setters.
 * @see #setPropagationBehavior
 * @see #setIsolationLevel
 * @see #setTimeout
 * @see #setReadOnly
 * @see #setName
 */
public DefaultTransactionDefinition(TransactionDefinition other) {
	this.propagationBehavior = other.getPropagationBehavior();
	this.isolationLevel = other.getIsolationLevel();
	this.timeout = other.getTimeout();
	this.readOnly = other.isReadOnly();
	this.name = other.getName();
}
 
Example 5
Source File: DefaultTransactionDefinition.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Copy constructor. Definition can be modified through bean property setters.
 * @see #setPropagationBehavior
 * @see #setIsolationLevel
 * @see #setTimeout
 * @see #setReadOnly
 * @see #setName
 */
public DefaultTransactionDefinition(TransactionDefinition other) {
	this.propagationBehavior = other.getPropagationBehavior();
	this.isolationLevel = other.getIsolationLevel();
	this.timeout = other.getTimeout();
	this.readOnly = other.isReadOnly();
	this.name = other.getName();
}
 
Example 6
Source File: DefaultTransactionDefinition.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Copy constructor. Definition can be modified through bean property setters.
 * @see #setPropagationBehavior
 * @see #setIsolationLevel
 * @see #setTimeout
 * @see #setReadOnly
 * @see #setName
 */
public DefaultTransactionDefinition(TransactionDefinition other) {
	this.propagationBehavior = other.getPropagationBehavior();
	this.isolationLevel = other.getIsolationLevel();
	this.timeout = other.getTimeout();
	this.readOnly = other.isReadOnly();
	this.name = other.getName();
}
 
Example 7
Source File: DefaultTransactionDefinition.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Copy constructor. Definition can be modified through bean property setters.
 * @see #setPropagationBehavior
 * @see #setIsolationLevel
 * @see #setTimeout
 * @see #setReadOnly
 * @see #setName
 */
public DefaultTransactionDefinition(TransactionDefinition other) {
	this.propagationBehavior = other.getPropagationBehavior();
	this.isolationLevel = other.getIsolationLevel();
	this.timeout = other.getTimeout();
	this.readOnly = other.isReadOnly();
	this.name = other.getName();
}