Search in sources :

Example 1 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project cucumber-jvm by cucumber.

the class SpringFactoryTest method shouldGiveUsNewInstancesOfGlueScopeClassesForEachScenario.

@Test
public void shouldGiveUsNewInstancesOfGlueScopeClassesForEachScenario() {
    final ObjectFactory factory = new SpringFactory();
    factory.addClass(BellyStepdefs.class);
    factory.addClass(AutowiresPlatformTransactionManager.class);
    // Scenario 1
    factory.start();
    final PlatformTransactionManager o1 = factory.getInstance(AutowiresPlatformTransactionManager.class).getTransactionManager();
    factory.stop();
    // Scenario 2
    factory.start();
    final PlatformTransactionManager o2 = factory.getInstance(AutowiresPlatformTransactionManager.class).getTransactionManager();
    factory.stop();
    assertNotNull(o1);
    assertNotNull(o2);
    assertNotSame(o1, o2);
}
Also used : ObjectFactory(cucumber.api.java.ObjectFactory) AutowiresPlatformTransactionManager(cucumber.runtime.java.spring.commonglue.AutowiresPlatformTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) AutowiresPlatformTransactionManager(cucumber.runtime.java.spring.commonglue.AutowiresPlatformTransactionManager) Test(org.junit.Test)

Example 2 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.

the class TestContextTransactionUtils method retrieveTransactionManager.

/**
	 * Retrieve the {@linkplain PlatformTransactionManager transaction manager}
	 * to use for the supplied {@linkplain TestContext test context}.
	 * <p>The following algorithm is used to retrieve the transaction manager
	 * from the {@link org.springframework.context.ApplicationContext ApplicationContext}
	 * of the supplied test context:
	 * <ol>
	 * <li>Look up the transaction manager by type and explicit name, if the supplied
	 * {@code name} is non-empty, throwing a {@link BeansException} if the named
	 * transaction manager does not exist.
	 * <li>Attempt to look up the single transaction manager by type.
	 * <li>Attempt to look up the <em>primary</em> transaction manager by type.
	 * <li>Attempt to look up the transaction manager via a
	 * {@link TransactionManagementConfigurer}, if present.
	 * <li>Attempt to look up the transaction manager by type and the
	 * {@linkplain #DEFAULT_TRANSACTION_MANAGER_NAME default transaction manager
	 * name}.
	 * @param testContext the test context for which the transaction manager
	 * should be retrieved; never {@code null}
	 * @param name the name of the transaction manager to retrieve; may be
	 * {@code null} or <em>empty</em>
	 * @return the transaction manager to use, or {@code null} if not found
	 * @throws BeansException if an error occurs while retrieving an explicitly
	 * named transaction manager
	 * @throws IllegalStateException if more than one TransactionManagementConfigurer
	 * exists in the ApplicationContext
	 */
public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext, String name) {
    Assert.notNull(testContext, "TestContext must not be null");
    BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
    try {
        // look up by type and explicit name
        if (StringUtils.hasText(name)) {
            return bf.getBean(name, PlatformTransactionManager.class);
        }
    } catch (BeansException ex) {
        logger.error(String.format("Failed to retrieve transaction manager named '%s' for test context %s", name, testContext), ex);
        throw ex;
    }
    try {
        if (bf instanceof ListableBeanFactory) {
            ListableBeanFactory lbf = (ListableBeanFactory) bf;
            // look up single bean by type
            Map<String, PlatformTransactionManager> txMgrs = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, PlatformTransactionManager.class);
            if (txMgrs.size() == 1) {
                return txMgrs.values().iterator().next();
            }
            try {
                // look up single bean by type, with support for 'primary' beans
                return bf.getBean(PlatformTransactionManager.class);
            } catch (BeansException ex) {
                logBeansException(testContext, ex, PlatformTransactionManager.class);
            }
            // look up single TransactionManagementConfigurer
            Map<String, TransactionManagementConfigurer> configurers = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, TransactionManagementConfigurer.class);
            Assert.state(configurers.size() <= 1, "Only one TransactionManagementConfigurer may exist in the ApplicationContext");
            if (configurers.size() == 1) {
                return configurers.values().iterator().next().annotationDrivenTransactionManager();
            }
        }
        // look up by type and default name
        return bf.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class);
    } catch (BeansException ex) {
        logBeansException(testContext, ex, PlatformTransactionManager.class);
        return null;
    }
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) TransactionManagementConfigurer(org.springframework.transaction.annotation.TransactionManagementConfigurer) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) BeansException(org.springframework.beans.BeansException)

Example 3 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.

the class TransactionAspectSupport method determineQualifiedTransactionManager.

private PlatformTransactionManager determineQualifiedTransactionManager(String qualifier) {
    PlatformTransactionManager txManager = this.transactionManagerCache.get(qualifier);
    if (txManager == null) {
        txManager = BeanFactoryAnnotationUtils.qualifiedBeanOfType(this.beanFactory, PlatformTransactionManager.class, qualifier);
        this.transactionManagerCache.putIfAbsent(qualifier, txManager);
    }
    return txManager;
}
Also used : CallbackPreferringPlatformTransactionManager(org.springframework.transaction.support.CallbackPreferringPlatformTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 4 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project spring-boot by spring-projects.

the class TransactionAutoConfigurationTests method singleTransactionManager.

@Test
public void singleTransactionManager() {
    load(DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class);
    PlatformTransactionManager transactionManager = this.context.getBean(PlatformTransactionManager.class);
    TransactionTemplate transactionTemplate = this.context.getBean(TransactionTemplate.class);
    assertThat(transactionTemplate.getTransactionManager()).isSameAs(transactionManager);
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 5 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project grails-core by grails.

the class ChainedTransactionManagerTests method shouldRollbackAllTransactionManagers.

@Test
public void shouldRollbackAllTransactionManagers() throws Exception {
    PlatformTransactionManager first = createNonFailingTransactionManager("first");
    PlatformTransactionManager second = createNonFailingTransactionManager("second");
    setupTransactionManagers(first, second);
    createAndRollbackTransaction();
    assertThat(first, wasRolledback());
    assertThat(second, wasRolledback());
}
Also used : TestPlatformTransactionManager(org.grails.transaction.ChainedTransactionManagerTests.TestPlatformTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Aggregations

PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)74 Test (org.junit.jupiter.api.Test)24 TransactionStatus (org.springframework.transaction.TransactionStatus)15 Test (org.junit.Test)14 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 BeanFactory (org.springframework.beans.factory.BeanFactory)8 Method (java.lang.reflect.Method)7 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)7 TransactionException (org.springframework.transaction.TransactionException)7 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)7 StreamCapableTransactionalOperationAdapter (org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)6 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 DataSource (javax.sql.DataSource)5 ServiceException (org.broadleafcommerce.common.exception.ServiceException)5 PersistenceResponse (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)5 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)5