Browse Source

Honor TxMgmtConfigurer when @Primary tx mgr is present in TCF

Prior to this commit, the Spring TestContext Framework (TCF) favored a
@Primary transaction manger over one configured via the
TransactionManagementConfigurer API, and this contradicts the behavior
in production Spring applications.

This commit aligns the transaction manger lookup within the TCF so that
a transaction manger configured via the TransactionManagementConfigurer
API is properly favored over a @Primary transaction manager.

Closes gh-24869
pull/25047/head
Sam Brannen 5 years ago
parent
commit
613bd3be1d
  1. 18
      spring-test/src/main/java/org/springframework/test/context/transaction/TestContextTransactionUtils.java
  2. 23
      spring-test/src/test/java/org/springframework/test/context/transaction/manager/LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests.java

18
spring-test/src/main/java/org/springframework/test/context/transaction/TestContextTransactionUtils.java

@ -145,9 +145,9 @@ public abstract class TestContextTransactionUtils { @@ -145,9 +145,9 @@ public abstract class TestContextTransactionUtils {
* {@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 <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}.
@ -190,14 +190,6 @@ public abstract class TestContextTransactionUtils { @@ -190,14 +190,6 @@ public abstract class TestContextTransactionUtils {
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);
@ -210,6 +202,14 @@ public abstract class TestContextTransactionUtils { @@ -210,6 +202,14 @@ public abstract class TestContextTransactionUtils {
"is not a PlatformTransactionManager: " + tm);
return (PlatformTransactionManager) tm;
}
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

23
spring-test/src/test/java/org/springframework/test/context/transaction/manager/LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests.java

@ -34,9 +34,9 @@ import org.springframework.transaction.testfixture.CallCountingTransactionManage @@ -34,9 +34,9 @@ import org.springframework.transaction.testfixture.CallCountingTransactionManage
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test that verifies the current behavior for transaction manager
* lookups when one transaction manager is {@link Primary @Primary} and an
* additional transaction manager is configured via the
* Integration test that verifies the behavior for transaction manager lookups
* when one transaction manager is {@link Primary @Primary} and an additional
* transaction manager is configured via the
* {@link TransactionManagementConfigurer} API.
*
* @author Sam Brannen
@ -44,7 +44,6 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -44,7 +44,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@SpringJUnitConfig
@Transactional
// TODO Update assertions once https://github.com/spring-projects/spring-framework/issues/24869 is fixed.
class LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests {
@Autowired
@ -57,28 +56,28 @@ class LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests { @@ -57,28 +56,28 @@ class LookUpTxMgrViaTransactionManagementConfigurerWithPrimaryTxMgrTests {
@Test
void transactionalTest() {
assertThat(primary.begun).isEqualTo(1);
assertThat(primary.inflight).isEqualTo(1);
assertThat(primary.begun).isEqualTo(0);
assertThat(primary.inflight).isEqualTo(0);
assertThat(primary.commits).isEqualTo(0);
assertThat(primary.rollbacks).isEqualTo(0);
assertThat(annotationDriven.begun).isEqualTo(0);
assertThat(annotationDriven.inflight).isEqualTo(0);
assertThat(annotationDriven.begun).isEqualTo(1);
assertThat(annotationDriven.inflight).isEqualTo(1);
assertThat(annotationDriven.commits).isEqualTo(0);
assertThat(annotationDriven.rollbacks).isEqualTo(0);
}
@AfterTransaction
void afterTransaction() {
assertThat(primary.begun).isEqualTo(1);
assertThat(primary.begun).isEqualTo(0);
assertThat(primary.inflight).isEqualTo(0);
assertThat(primary.commits).isEqualTo(0);
assertThat(primary.rollbacks).isEqualTo(1);
assertThat(primary.rollbacks).isEqualTo(0);
assertThat(annotationDriven.begun).isEqualTo(0);
assertThat(annotationDriven.begun).isEqualTo(1);
assertThat(annotationDriven.inflight).isEqualTo(0);
assertThat(annotationDriven.commits).isEqualTo(0);
assertThat(annotationDriven.rollbacks).isEqualTo(0);
assertThat(annotationDriven.rollbacks).isEqualTo(1);
}

Loading…
Cancel
Save