use of cn.taketoday.transaction.TransactionManager in project today-infrastructure by TAKETODAY.
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 cn.taketoday.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 transaction manager via a
* {@link TransactionManagementConfigurer}, if present.
* <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 by type and the
* {@linkplain #DEFAULT_TRANSACTION_MANAGER_NAME default transaction manager
* name}.
* </ol>
*
* @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
*/
@Nullable
public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext, @Nullable 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("Failed to retrieve transaction manager named '{}' for test context {}", name, testContext, ex);
throw ex;
}
try {
// Look up single TransactionManagementConfigurer
Map<String, TransactionManagementConfigurer> configurers = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, TransactionManagementConfigurer.class);
Assert.state(configurers.size() <= 1, "Only one TransactionManagementConfigurer may exist in the ApplicationContext");
if (configurers.size() == 1) {
TransactionManager tm = configurers.values().iterator().next().annotationDrivenTransactionManager();
Assert.state(tm instanceof PlatformTransactionManager, () -> "Transaction manager specified via TransactionManagementConfigurer " + "is not a PlatformTransactionManager: " + tm);
return (PlatformTransactionManager) tm;
}
// Look up single bean by type
Map<String, PlatformTransactionManager> txMgrs = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, 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 by type and default name
return bf.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class);
} catch (BeansException ex) {
logBeansException(testContext, ex, PlatformTransactionManager.class);
return null;
}
}
use of cn.taketoday.transaction.TransactionManager in project today-infrastructure by TAKETODAY.
the class TransactionInterceptorTests method determineTransactionManagerWithBeanNameSeveralTimes.
@Test
public void determineTransactionManagerWithBeanNameSeveralTimes() {
BeanFactory beanFactory = mock(BeanFactory.class);
TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName("fooTransactionManager", beanFactory);
PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager");
DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
TransactionManager actual = ti.determineTransactionManager(attribute);
assertThat(actual).isSameAs(txManager);
// Call again, should be cached
TransactionManager actual2 = ti.determineTransactionManager(attribute);
assertThat(actual2).isSameAs(txManager);
verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class);
}
Aggregations