Test status quo for @Primary + TransactionManagementConfigurer

This commit introduces tests for the status quo in production support
for multiple transaction managers registered as @Primary and via the
TransactionManagementConfigurer API.

See gh-24869
This commit is contained in:
Sam Brannen 2020-04-06 15:16:31 +02:00
parent dc5ddffd03
commit 144b0e1437
1 changed files with 86 additions and 1 deletions

View File

@ -31,6 +31,7 @@ import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.context.annotation.Primary;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
@ -106,7 +107,6 @@ public class EnableTransactionManagementTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
EnableTxConfig.class, MultiTxManagerConfig.class);
TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
CallCountingTransactionManager txManager = ctx.getBean("txManager", CallCountingTransactionManager.class);
CallCountingTransactionManager txManager2 = ctx.getBean("txManager2", CallCountingTransactionManager.class);
@ -122,6 +122,48 @@ public class EnableTransactionManagementTests {
ctx.close();
}
@Test
public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresentAndOneIsPrimary() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
EnableTxConfig.class, PrimaryMultiTxManagerConfig.class);
TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
CallCountingTransactionManager primary = ctx.getBean("primary", CallCountingTransactionManager.class);
CallCountingTransactionManager txManager2 = ctx.getBean("txManager2", CallCountingTransactionManager.class);
// invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
bean.findAllFoos();
assertThat(primary.begun).isEqualTo(1);
assertThat(primary.commits).isEqualTo(1);
assertThat(primary.rollbacks).isEqualTo(0);
assertThat(txManager2.begun).isEqualTo(0);
assertThat(txManager2.commits).isEqualTo(0);
assertThat(txManager2.rollbacks).isEqualTo(0);
ctx.close();
}
@Test
public void txManagerIsResolvedCorrectlyWithTxMgmtConfigurerAndPrimaryPresent() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
EnableTxConfig.class, PrimaryTxManagerAndTxMgmtConfigurerConfig.class);
TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
CallCountingTransactionManager primary = ctx.getBean("primary", CallCountingTransactionManager.class);
CallCountingTransactionManager annotationDriven = ctx.getBean("annotationDrivenTransactionManager", CallCountingTransactionManager.class);
// invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
bean.findAllFoos();
assertThat(primary.begun).isEqualTo(0);
assertThat(primary.commits).isEqualTo(0);
assertThat(primary.rollbacks).isEqualTo(0);
assertThat(annotationDriven.begun).isEqualTo(1);
assertThat(annotationDriven.commits).isEqualTo(1);
assertThat(annotationDriven.rollbacks).isEqualTo(0);
ctx.close();
}
/**
* A cheap test just to prove that in ASPECTJ mode, the AnnotationTransactionAspect does indeed
* get loaded -- or in this case, attempted to be loaded at which point the test fails.
@ -295,6 +337,49 @@ public class EnableTransactionManagementTests {
}
@Configuration
static class PrimaryMultiTxManagerConfig {
@Bean
public TransactionalTestBean testBean() {
return new TransactionalTestBean();
}
@Bean
@Primary
public PlatformTransactionManager primary() {
return new CallCountingTransactionManager();
}
@Bean
public PlatformTransactionManager txManager2() {
return new CallCountingTransactionManager();
}
}
@Configuration
static class PrimaryTxManagerAndTxMgmtConfigurerConfig implements TransactionManagementConfigurer {
@Bean
public TransactionalTestBean testBean() {
return new TransactionalTestBean();
}
@Bean
@Primary
public PlatformTransactionManager primary() {
return new CallCountingTransactionManager();
}
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new CallCountingTransactionManager();
}
}
@Configuration
@EnableTransactionManagement
static class Spr11915Config {