Support Propagation.NEVER for disabling test-managed transactions
Prior to this commit only Propagation.NOT_SUPPORTED was supported for disabling test-managed transactions via the `propagation` attribute of `@Transactional`. This commit allows users to specify Propagation.NOT_SUPPORTED or Propagation.NEVER to disable test-managed transactions. Closes gh-25909
This commit is contained in:
parent
33fcba55d1
commit
e5ae2cb0fe
|
@ -79,6 +79,7 @@ import org.springframework.util.StringUtils;
|
||||||
* <em>are</em> annotated with {@code @Transactional} but have the
|
* <em>are</em> annotated with {@code @Transactional} but have the
|
||||||
* {@link Transactional#propagation propagation} type set to
|
* {@link Transactional#propagation propagation} type set to
|
||||||
* {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED}
|
* {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED}
|
||||||
|
* or {@link org.springframework.transaction.annotation.Propagation#NEVER NEVER}
|
||||||
* will not be run within a transaction.
|
* will not be run within a transaction.
|
||||||
*
|
*
|
||||||
* <h3>Declarative Rollback and Commit Behavior</h3>
|
* <h3>Declarative Rollback and Commit Behavior</h3>
|
||||||
|
@ -123,7 +124,8 @@ import org.springframework.util.StringUtils;
|
||||||
* <tr><th>Attribute</th><th>Supported for test-managed transactions</th></tr>
|
* <tr><th>Attribute</th><th>Supported for test-managed transactions</th></tr>
|
||||||
* <tr><td>{@link Transactional#value value} and {@link Transactional#transactionManager transactionManager}</td><td>yes</td></tr>
|
* <tr><td>{@link Transactional#value value} and {@link Transactional#transactionManager transactionManager}</td><td>yes</td></tr>
|
||||||
* <tr><td>{@link Transactional#propagation propagation}</td>
|
* <tr><td>{@link Transactional#propagation propagation}</td>
|
||||||
* <td>only {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED} is supported</td></tr>
|
* <td>only {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED}
|
||||||
|
* and {@link org.springframework.transaction.annotation.Propagation#NEVER NEVER} are supported</td></tr>
|
||||||
* <tr><td>{@link Transactional#isolation isolation}</td><td>no</td></tr>
|
* <tr><td>{@link Transactional#isolation isolation}</td><td>no</td></tr>
|
||||||
* <tr><td>{@link Transactional#timeout timeout}</td><td>no</td></tr>
|
* <tr><td>{@link Transactional#timeout timeout}</td><td>no</td></tr>
|
||||||
* <tr><td>{@link Transactional#readOnly readOnly}</td><td>no</td></tr>
|
* <tr><td>{@link Transactional#readOnly readOnly}</td><td>no</td></tr>
|
||||||
|
@ -213,7 +215,8 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||||
"] found for test context " + testContext);
|
"] found for test context " + testContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
|
if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED ||
|
||||||
|
transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -134,7 +134,7 @@ class ProgrammaticTxMgmtTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
@Transactional(propagation = Propagation.NEVER)
|
||||||
void startTxWithNonExistentTransactionContext() {
|
void startTxWithNonExistentTransactionContext() {
|
||||||
assertThatIllegalStateException().isThrownBy(TestTransaction::start);
|
assertThatIllegalStateException().isThrownBy(TestTransaction::start);
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ class ProgrammaticTxMgmtTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
@Transactional(propagation = Propagation.NEVER)
|
||||||
void endTxWithNonExistentTransactionContext() {
|
void endTxWithNonExistentTransactionContext() {
|
||||||
assertThatIllegalStateException().isThrownBy(TestTransaction::end);
|
assertThatIllegalStateException().isThrownBy(TestTransaction::end);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5056,7 +5056,7 @@ hierarchy runs within a transaction. Test methods that are not annotated with
|
||||||
that `@Transactional` is not supported on test lifecycle methods — for example, methods
|
that `@Transactional` is not supported on test lifecycle methods — for example, methods
|
||||||
annotated with JUnit Jupiter's `@BeforeAll`, `@BeforeEach`, etc. Furthermore, tests that
|
annotated with JUnit Jupiter's `@BeforeAll`, `@BeforeEach`, etc. Furthermore, tests that
|
||||||
are annotated with `@Transactional` but have the `propagation` attribute set to
|
are annotated with `@Transactional` but have the `propagation` attribute set to
|
||||||
`NOT_SUPPORTED` are not run within a transaction.
|
`NOT_SUPPORTED` or `NEVER` are not run within a transaction.
|
||||||
|
|
||||||
[[testcontext-tx-attribute-support]]
|
[[testcontext-tx-attribute-support]]
|
||||||
.`@Transactional` attribute support
|
.`@Transactional` attribute support
|
||||||
|
@ -5065,7 +5065,7 @@ are annotated with `@Transactional` but have the `propagation` attribute set to
|
||||||
|
|
||||||
|`value` and `transactionManager` |yes
|
|`value` and `transactionManager` |yes
|
||||||
|
|
||||||
|`propagation` |only `Propagation.NOT_SUPPORTED` is supported
|
|`propagation` |only `Propagation.NOT_SUPPORTED` and `Propagation.NEVER` are supported
|
||||||
|
|
||||||
|`isolation` |no
|
|`isolation` |no
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue