Java Code Examples for org.springframework.transaction.TransactionDefinition#PROPAGATION_NOT_SUPPORTED

The following examples show how to use org.springframework.transaction.TransactionDefinition#PROPAGATION_NOT_SUPPORTED . 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: AbstractTransactionalSpringContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Overridden to populate transaction definition from annotations.
 */
@Override
public void runBare() throws Throwable {
	final Method testMethod = getTestMethod();

	TransactionDefinition explicitTransactionDefinition = this.transactionAttributeSource.getTransactionAttribute(
		testMethod, getClass());
	if (explicitTransactionDefinition != null) {
		this.logger.info("Custom transaction definition [" + explicitTransactionDefinition + "] for test method ["
				+ getName() + "].");
		setTransactionDefinition(explicitTransactionDefinition);
	}

	if (this.transactionDefinition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
		preventTransaction();
	}

	super.runBare();
}
 
Example 2
Source File: TransactionalTestExecutionListener.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * If the test method of the supplied {@linkplain TestContext test context}
 * is configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction} methods and start a new
 * transaction.
 * <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
 * {@code @BeforeTransaction} methods will not be invoked, and a transaction
 * will not be started.
 * @see org.springframework.transaction.annotation.Transactional
 * @see #getTransactionManager(TestContext, String)
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
	Method testMethod = testContext.getTestMethod();
	Class<?> testClass = testContext.getTestClass();
	Assert.notNull(testMethod, "Test method of supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	Assert.state(txContext == null, "Cannot start new transaction without ending existing transaction");

	PlatformTransactionManager tm = null;
	TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod, testClass);

	if (transactionAttribute != null) {
		transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(testContext,
			transactionAttribute);

		if (logger.isDebugEnabled()) {
			logger.debug("Explicit transaction definition [" + transactionAttribute +
					"] found for test context " + testContext);
		}

		if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			return;
		}

		tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
		Assert.state(tm != null,
				() -> "Failed to retrieve PlatformTransactionManager for @Transactional test: " + testContext);
	}

	if (tm != null) {
		txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
		runBeforeTransactionMethods(testContext);
		txContext.startTransaction();
		TransactionContextHolder.setCurrentTransactionContext(txContext);
	}
}
 
Example 3
Source File: TransactionalTestExecutionListener.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * If the test method of the supplied {@linkplain TestContext test context}
 * is configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction} methods and start a new
 * transaction.
 * <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
 * {@code @BeforeTransaction} methods will not be invoked, and a transaction
 * will not be started.
 * @see org.springframework.transaction.annotation.Transactional
 * @see #getTransactionManager(TestContext, String)
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
	Method testMethod = testContext.getTestMethod();
	Class<?> testClass = testContext.getTestClass();
	Assert.notNull(testMethod, "Test method of supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	Assert.state(txContext == null, "Cannot start new transaction without ending existing transaction");

	PlatformTransactionManager tm = null;
	TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod, testClass);

	if (transactionAttribute != null) {
		transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(testContext,
			transactionAttribute);

		if (logger.isDebugEnabled()) {
			logger.debug("Explicit transaction definition [" + transactionAttribute +
					"] found for test context " + testContext);
		}

		if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			return;
		}

		tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
		Assert.state(tm != null,
				() -> "Failed to retrieve PlatformTransactionManager for @Transactional test: " + testContext);
	}

	if (tm != null) {
		txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
		runBeforeTransactionMethods(testContext);
		txContext.startTransaction();
		TransactionContextHolder.setCurrentTransactionContext(txContext);
	}
}
 
Example 4
Source File: TransactionalTestExecutionListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * If the test method of the supplied {@linkplain TestContext test context}
 * is configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction} methods and start a new
 * transaction.
 * <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
 * {@code @BeforeTransaction} methods will not be invoked, and a transaction
 * will not be started.
 * @see org.springframework.transaction.annotation.Transactional
 * @see #getTransactionManager(TestContext, String)
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
	final Method testMethod = testContext.getTestMethod();
	final Class<?> testClass = testContext.getTestClass();
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	if (txContext != null) {
		throw new IllegalStateException("Cannot start a new transaction without ending the existing transaction.");
	}

	PlatformTransactionManager tm = null;
	TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod, testClass);

	if (transactionAttribute != null) {
		transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(testContext,
			transactionAttribute);

		if (logger.isDebugEnabled()) {
			logger.debug("Explicit transaction definition [" + transactionAttribute + "] found for test context "
					+ testContext);
		}

		if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			return;
		}

		tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
	}

	if (tm != null) {
		txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
		runBeforeTransactionMethods(testContext);
		txContext.startTransaction();
		TransactionContextHolder.setCurrentTransactionContext(txContext);
	}
}
 
Example 5
Source File: JtaUtil.java    From iaf with Apache License 2.0 5 votes vote down vote up
public static String getTransactionAttributeString(int transactionAttribute) {
	if (transactionAttribute<0 || transactionAttribute>=transactionAttributes.length) {
		return "UnknownTransactionAttribute:"+transactionAttribute;
	}
	switch (transactionAttribute) {
		case TransactionDefinition.PROPAGATION_MANDATORY:
		return TRANSACTION_ATTRIBUTE_MANDATORY_STR;
   
		case TransactionDefinition.PROPAGATION_NEVER:
		return TRANSACTION_ATTRIBUTE_NEVER_STR;
   
		case TransactionDefinition.PROPAGATION_NOT_SUPPORTED:
		return TRANSACTION_ATTRIBUTE_NOT_SUPPORTED_STR;
   
		case TransactionDefinition.PROPAGATION_SUPPORTS:
		return TRANSACTION_ATTRIBUTE_SUPPORTS_STR;
   
		case TransactionDefinition.PROPAGATION_REQUIRED:
		return TRANSACTION_ATTRIBUTE_REQUIRED_STR;
   
		case TransactionDefinition.PROPAGATION_REQUIRES_NEW:
		return TRANSACTION_ATTRIBUTE_REQUIRES_NEW_STR;
   
		default:
		return null;
	}
}
 
Example 6
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);
		}
	}
}