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

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

Introduction

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

Prototype

@Nullable
String getQualifier();

Source Link

Document

Return a qualifier value associated with this transaction attribute.

Usage

From source file:com.google.code.guice.repository.spi.CompositeTransactionInterceptor.java

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    // persistence unit name detection
    Class<?> targetClass = invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null;
    TransactionAttribute txAttr = transactionAttributeSource.getTransactionAttribute(invocation.getMethod(),
            targetClass);//from  www .j ava2 s  . co m
    String persistenceUnitName = null;

    if (txAttr != null) {
        persistenceUnitName = txAttr.getQualifier();
    }

    PersistenceUnitConfiguration configuration = configurationManager.getConfiguration(persistenceUnitName);
    return configuration.getTransactionInterceptor().invoke(invocation);
}

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

@SuppressWarnings("serial")
@Override/*from w w w  .  j  a  v a2 s.  c om*/
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.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManagerTest.java

@Test
public void testTransactionalAttributesOnStaticMethod1() throws Throwable {
    try {/*  ww  w.j a va  2 s.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./*from   w  w  w .  java 2  s.  c o m*/
 * <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);
    }
}

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

/**
 * Determine the specific transaction manager to use for the given transaction.
 *//*from  ww w . ja  v a2  s. c o  m*/
@Nullable
protected PlatformTransactionManager determineTransactionManager(@Nullable TransactionAttribute txAttr) {
    // Do not attempt to lookup tx manager if no tx attributes are set
    if (txAttr == null || this.beanFactory == null) {
        return getTransactionManager();
    }

    String qualifier = txAttr.getQualifier();
    if (StringUtils.hasText(qualifier)) {
        return determineQualifiedTransactionManager(this.beanFactory, qualifier);
    } else if (StringUtils.hasText(this.transactionManagerBeanName)) {
        return determineQualifiedTransactionManager(this.beanFactory, this.transactionManagerBeanName);
    } else {
        PlatformTransactionManager defaultTransactionManager = getTransactionManager();
        if (defaultTransactionManager == null) {
            defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY);
            if (defaultTransactionManager == null) {
                defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class);
                this.transactionManagerCache.putIfAbsent(DEFAULT_TRANSACTION_MANAGER_KEY,
                        defaultTransactionManager);
            }
        }
        return defaultTransactionManager;
    }
}