Example usage for org.springframework.transaction.interceptor TransactionAttribute getPropagationBehavior

List of usage examples for org.springframework.transaction.interceptor TransactionAttribute getPropagationBehavior

Introduction

In this page you can find the example usage for org.springframework.transaction.interceptor TransactionAttribute getPropagationBehavior.

Prototype

default int getPropagationBehavior() 

Source Link

Document

Return the propagation behavior.

Usage

From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManagerTest.java

@Test
public void testTransactionalAttributesOnStaticMethod1() throws Throwable {
    try {/*from  w ww  .  java2s. c  o m*/
        Method createNewAuthorStaticMethod = Author.class.getDeclaredMethod("createNewAuthor",
                new Class[] { String.class });

        TransactionAttributeSource transactionAttributeSource
        //         = new AnnotationTransactionAttributeSource( false);
                = annotationTransactionAspect.getTransactionAttributeSource();

        TransactionAttribute transactionAttribute = transactionAttributeSource
                .getTransactionAttribute(createNewAuthorStaticMethod, Author.class);

        /* Made addition to ExtendedAnnotationTransactionAspect, so no matter of attribute value of 
         * @Transactional annotation, noRollBack method instead of rollback method of 
         * Slim3PlatformTransactionManager class will be called when encountering 
         * ConcurrentModificationException exception and DeadlineExceededException exception. 
        Assert.assertFalse( 
              transactionAttribute.rollbackOn( new ConcurrentModificationException()));
        Assert.assertFalse( 
              transactionAttribute.rollbackOn( new DeadlineExceededException()));
        */

        String name = transactionAttribute.getName();
        String message = String.format(
                "Attribution values of @Transactional annotation on %1$s:" + "%n%2$cvalue=\"%3$s\"",
                createNewAuthorStaticMethod.toString(), '\t', (name == null ? "" : name));
        int isolationLevel = transactionAttribute.getIsolationLevel();
        Assert.assertEquals(TransactionAttribute.ISOLATION_DEFAULT, isolationLevel);
        switch (isolationLevel) {
        case TransactionAttribute.ISOLATION_DEFAULT:
            message = message + String.format("%n%1$cisolation=ISOLATION_DEFAULT", '\t');
            break;
        case TransactionAttribute.ISOLATION_READ_COMMITTED:
            message = message + String.format("%n%1$cisolation=ISOLATION_READ_COMMITTED", '\t');
            break;
        case TransactionAttribute.ISOLATION_READ_UNCOMMITTED:
            message = message + String.format("%n%1$cisolation=ISOLATION_READ_UNCOMMITTED", '\t');
            break;
        case TransactionAttribute.ISOLATION_REPEATABLE_READ:
            message = message + String.format("%n%1$cisolation=ISOLATION_REPEATABLE_READ", '\t');
            break;
        case TransactionAttribute.ISOLATION_SERIALIZABLE:
            message = message + String.format("%n%1$cisolation=ISOLATION_SERIALIZABLE", '\t');
            break;
        default:
            Assert.fail("Unrecognizable isolation level.");
        } // switch

        int propagationBehavior = transactionAttribute.getPropagationBehavior();
        //Default propagation behavior seems to be TransactionAttribute.PROPAGATION_REQUIRED
        Assert.assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, propagationBehavior);
        switch (propagationBehavior) {
        case TransactionAttribute.PROPAGATION_MANDATORY:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_MANDATORY", '\t');
            break;
        case TransactionAttribute.PROPAGATION_NESTED:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_NESTED", '\t');
            break;
        case TransactionAttribute.PROPAGATION_NEVER:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_NEVER", '\t');
            break;
        case TransactionAttribute.PROPAGATION_NOT_SUPPORTED:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_NOT_SUPPORTED", '\t');
            break;
        case TransactionAttribute.PROPAGATION_REQUIRED:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_REQUIRED", '\t');
            break;
        case TransactionAttribute.PROPAGATION_REQUIRES_NEW:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_REQUIRES_NEW", '\t');
            break;
        case TransactionAttribute.PROPAGATION_SUPPORTS:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_SUPPORTS", '\t');
            break;
        default:
            Assert.fail("Unrecognizable propagation behavior.");
        } // switch

        String qualifier = transactionAttribute.getQualifier();
        message = message + String.format("%n%1$cqualifier=%2$s", '\t', qualifier);

        int timeout = transactionAttribute.getTimeout();
        Assert.assertEquals(TransactionAttribute.TIMEOUT_DEFAULT, timeout);
        message = message + String.format("%n%1$ctimeout=%2$d", '\t', timeout);

        boolean readOnlyFlag = transactionAttribute.isReadOnly();
        Assert.assertFalse(readOnlyFlag);
        message = message + String.format("%n%1$creadOnly=%2$s", '\t', String.valueOf(readOnlyFlag));

        if (logger.isDebugEnabled()) {
            logger.debug(message);
        }
    } catch (Throwable throwable) {
        throw throwable;
    }
}

From source file:org.springframework.test.context.transaction.TransactionalTestExecutionListener.java

/**
 * 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.//ww w . j av a2 s .c om
 * <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, "The test method of the supplied TestContext must not be null");

    TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
    Assert.state(txContext == null, "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());
        Assert.state(tm != null, () -> String.format(
                "Failed to retrieve PlatformTransactionManager for @Transactional test for test context %s.",
                testContext));
    }

    if (tm != null) {
        txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
        runBeforeTransactionMethods(testContext);
        txContext.startTransaction();
        TransactionContextHolder.setCurrentTransactionContext(txContext);
    }
}