Example usage for org.springframework.transaction.interceptor DelegatingTransactionAttribute DelegatingTransactionAttribute

List of usage examples for org.springframework.transaction.interceptor DelegatingTransactionAttribute DelegatingTransactionAttribute

Introduction

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

Prototype

public DelegatingTransactionAttribute(TransactionAttribute targetAttribute) 

Source Link

Document

Create a DelegatingTransactionAttribute for the given target attribute.

Usage

From source file:com.github.springtestdbunit.SingleTransactionTestExecutionListener.java

@SuppressWarnings("serial")
@Override/*from   w w  w  .  j  av a  2  s  .  c  o  m*/
public void beforeTestClass(TestContext testContext) throws Exception {
    super.prepareTestInstance(testContext);

    final Class<?> testClass = testContext.getTestClass();

    TransactionAttribute transactionAttribute = annotationParser.parseTransactionAnnotation(testClass);

    TransactionDefinition transactionDefinition = null;
    if (transactionAttribute != null) {
        transactionDefinition = new DelegatingTransactionAttribute(transactionAttribute) {
            public String getName() {
                return testClass.getName();
            }
        };
    }

    if (transactionDefinition != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Explicit transaction definition [" + transactionDefinition
                    + "] found for test context [" + testContext + "]");
        }
        String qualifier = transactionAttribute.getQualifier();
        PlatformTransactionManager tm;
        if (StringUtils.hasLength(qualifier)) {
            // Use autowire-capable factory in order to support extended qualifier matching
            // (only exposed on the internal BeanFactory, not on the ApplicationContext).
            BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
            tm = TransactionAspectUtils.getTransactionManager(bf, qualifier);
        } else {
            tm = getTransactionManager(testContext);
        }
        transactionContext = new TransactionContext(tm, transactionDefinition);
        startNewTransaction(testContext, transactionContext);
        logger.debug("Started transaction. Setting up database");
        runner.beforeTestMethod(new DbUnitTestContextAdapter(testContext));
    }
}

From source file:com.taobao.itest.listener.TransactionalListener.java

/**
 * If the test method of the supplied {@link TestContext test context} is
 * configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction methods} and start a new
 * transaction.//from w w  w. ja  v  a  2s.co m
 * <p>
 * Note that if a {@link BeforeTransaction @BeforeTransaction method} fails,
 * remaining {@link BeforeTransaction @BeforeTransaction methods} will not
 * be invoked, and a transaction will not be started.
 * 
 * @see org.springframework.transaction.annotation.Transactional
 * @see org.springframework.test.annotation.NotTransactional
 */
@SuppressWarnings("serial")
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
    final Method testMethod = testContext.getTestMethod();
    Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

    if (this.transactionContextCache.remove(testMethod) != null) {
        throw new IllegalStateException("Cannot start new transaction without ending existing transaction: "
                + "Invoke endTransaction() before startNewTransaction().");
    }

    if (testMethod.isAnnotationPresent(NotTransactional.class)) {
        return;
    }

    TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod,
            testContext.getTestClass());
    TransactionDefinition transactionDefinition = null;
    if (transactionAttribute != null) {
        transactionDefinition = new DelegatingTransactionAttribute(transactionAttribute) {
            public String getName() {
                return testMethod.getName();
            }
        };
    }

    if (transactionDefinition != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Explicit transaction definition [" + transactionDefinition
                    + "] found for test context [" + testContext + "]");
        }
        TransactionContext txContext = new TransactionContext(getTransactionManager(testContext),
                transactionDefinition);
        runBeforeTransactionMethods(testContext);
        startNewTransaction(testContext, txContext);
        this.transactionContextCache.put(testMethod, txContext);
    }
}

From source file:org.broadleafcommerce.test.MergeTransactionalTestExecutionListener.java

/**
 * If the test method of the supplied {@link TestContext test context} is
 * configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction methods} and start a new
 * transaction./*from   w w w.j  a  v  a 2  s .c o  m*/
 * <p>Note that if a {@link BeforeTransaction @BeforeTransaction method} fails,
 * remaining {@link BeforeTransaction @BeforeTransaction methods} will not
 * be invoked, and a transaction will not be started.
 * @see org.springframework.transaction.annotation.Transactional
 * @see org.springframework.test.annotation.NotTransactional
 */
@SuppressWarnings("serial")
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
    final Method testMethod = testContext.getTestMethod();
    Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

    if (this.transactionContextCache.remove(testMethod) != null) {
        throw new IllegalStateException("Cannot start new transaction without ending existing transaction: "
                + "Invoke endTransaction() before startNewTransaction().");
    }

    if (!testMethod.isAnnotationPresent(Transactional.class)) {
        return;
    }

    TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod,
            testContext.getTestClass());
    TransactionDefinition transactionDefinition = null;
    if (transactionAttribute != null) {
        transactionDefinition = new DelegatingTransactionAttribute(transactionAttribute) {
            public String getName() {
                return testMethod.getName();
            }
        };
    }

    if (transactionDefinition != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Explicit transaction definition [" + transactionDefinition
                    + "] found for test context [" + testContext + "]");
        }
        TransactionContext txContext = new TransactionContext(getTransactionManager(testContext),
                transactionDefinition);
        runBeforeTransactionMethods(testContext);
        startNewTransaction(testContext, txContext);
        this.transactionContextCache.put(testMethod, txContext);
    }
}

From source file:org.springframework.transaction.interceptor.TransactionAspectSupport.java

/**
 * Create a transaction if necessary based on the given TransactionAttribute.
 * <p>Allows callers to perform custom TransactionAttribute lookups through
 * the TransactionAttributeSource./*from  ww  w . j  a  va  2  s .  c om*/
 * @param txAttr the TransactionAttribute (may be {@code null})
 * @param joinpointIdentification the fully qualified method name
 * (used for monitoring and logging purposes)
 * @return a TransactionInfo object, whether or not a transaction was created.
 * The {@code hasTransaction()} method on TransactionInfo can be used to
 * tell if there was a transaction created.
 * @see #getTransactionAttributeSource()
 */
@SuppressWarnings("serial")
protected TransactionInfo createTransactionIfNecessary(@Nullable PlatformTransactionManager tm,
        @Nullable TransactionAttribute txAttr, final String joinpointIdentification) {

    // If no name specified, apply method identification as transaction name.
    if (txAttr != null && txAttr.getName() == null) {
        txAttr = new DelegatingTransactionAttribute(txAttr) {
            @Override
            public String getName() {
                return joinpointIdentification;
            }
        };
    }

    TransactionStatus status = null;
    if (txAttr != null) {
        if (tm != null) {
            status = tm.getTransaction(txAttr);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Skipping transactional joinpoint [" + joinpointIdentification
                        + "] because no transaction manager has been configured");
            }
        }
    }
    return prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
}