Skip to content

Commit 8ceac9c

Browse files
committed
Document supported @transactional attributes in the TCF
This commit documents which attributes in @transactional are supported for test-managed transactions in the Spring TestContext Framework (TCF). Closes gh-23149
1 parent da90390 commit 8ceac9c

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.transaction.TransactionDefinition;
4141
import org.springframework.transaction.TransactionStatus;
4242
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
43+
import org.springframework.transaction.annotation.Transactional;
4344
import org.springframework.transaction.interceptor.TransactionAttribute;
4445
import org.springframework.transaction.interceptor.TransactionAttributeSource;
4546
import org.springframework.util.Assert;
@@ -49,8 +50,7 @@
4950
/**
5051
* {@code TestExecutionListener} that provides support for executing tests
5152
* within <em>test-managed transactions</em> by honoring Spring's
52-
* {@link org.springframework.transaction.annotation.Transactional @Transactional}
53-
* annotation.
53+
* {@link Transactional @Transactional} annotation.
5454
*
5555
* <h3>Test-managed Transactions</h3>
5656
* <p><em>Test-managed transactions</em> are transactions that are managed
@@ -76,8 +76,7 @@
7676
* <em>not</em> annotated with {@code @Transactional} (at the class or method
7777
* level) will not be run within a transaction. Furthermore, tests that
7878
* <em>are</em> annotated with {@code @Transactional} but have the
79-
* {@link org.springframework.transaction.annotation.Transactional#propagation propagation}
80-
* type set to
79+
* {@link Transactional#propagation propagation} type set to
8180
* {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED}
8281
* will not be run within a transaction.
8382
*
@@ -109,15 +108,30 @@
109108
* {@code ApplicationContext} for the test. In case there are multiple
110109
* instances of {@code PlatformTransactionManager} within the test's
111110
* {@code ApplicationContext}, a <em>qualifier</em> may be declared via
112-
* {@link org.springframework.transaction.annotation.Transactional @Transactional}
113-
* (e.g., {@code @Transactional("myTxMgr")} or {@code @Transactional(transactionManger = "myTxMgr")},
114-
* or {@link org.springframework.transaction.annotation.TransactionManagementConfigurer
111+
* {@link Transactional @Transactional} (e.g., {@code @Transactional("myTxMgr")}
112+
* or {@code @Transactional(transactionManger = "myTxMgr")}, or
113+
* {@link org.springframework.transaction.annotation.TransactionManagementConfigurer
115114
* TransactionManagementConfigurer} can be implemented by an
116115
* {@link org.springframework.context.annotation.Configuration @Configuration}
117116
* class. See {@link TestContextTransactionUtils#retrieveTransactionManager}
118117
* for details on the algorithm used to look up a transaction manager in
119118
* the test's {@code ApplicationContext}.
120119
*
120+
* <h3>{@code @Transactional} Attribute Support</h3>
121+
* <table border="1">
122+
* <tr><th>Attribute</th><th>Supported for test-managed transactions</th></tr>
123+
* <tr><td>{@link Transactional#value value} and {@link Transactional#transactionManager transactionManager}</td><td>yes</td></tr>
124+
* <tr><td>{@link Transactional#propagation propagation}</td>
125+
* <td>only {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED} is supported</td></tr>
126+
* <tr><td>{@link Transactional#isolation isolation}</td><td>no</td></tr>
127+
* <tr><td>{@link Transactional#timeout timeout}</td><td>no</td></tr>
128+
* <tr><td>{@link Transactional#readOnly readOnly}</td><td>no</td></tr>
129+
* <tr><td>{@link Transactional#rollbackFor rollbackFor} and {@link Transactional#rollbackForClassName rollbackForClassName}</td>
130+
* <td>no: use {@link TestTransaction#flagForRollback()} instead</td></tr>
131+
* <tr><td>{@link Transactional#noRollbackFor noRollbackFor} and {@link Transactional#noRollbackForClassName noRollbackForClassName}</td>
132+
* <td>no: use {@link TestTransaction#flagForCommit()} instead</td></tr>
133+
* </table>
134+
*
121135
* @author Sam Brannen
122136
* @author Juergen Hoeller
123137
* @since 2.5
@@ -153,7 +167,7 @@ public final int getOrder() {
153167
* <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
154168
* {@code @BeforeTransaction} methods will not be invoked, and a transaction
155169
* will not be started.
156-
* @see org.springframework.transaction.annotation.Transactional
170+
* @see Transactional @Transactional
157171
* @see #getTransactionManager(TestContext, String)
158172
*/
159173
@Override

src/docs/asciidoc/testing.adoc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,14 +1025,16 @@ and can be used anywhere in the Spring Framework.
10251025

10261026
* `@Autowired`
10271027
* `@Qualifier`
1028+
* `@Value`
10281029
* `@Resource` (javax.annotation) if JSR-250 is present
10291030
* `@ManagedBean` (javax.annotation) if JSR-250 is present
10301031
* `@Inject` (javax.inject) if JSR-330 is present
10311032
* `@Named` (javax.inject) if JSR-330 is present
10321033
* `@PersistenceContext` (javax.persistence) if JPA is present
10331034
* `@PersistenceUnit` (javax.persistence) if JPA is present
10341035
* `@Required`
1035-
* `@Transactional`
1036+
* `@Transactional` (org.springframework.transaction.annotation)
1037+
_with <<testcontext-tx-attribute-support, limited attribute support>>_
10361038

10371039
.JSR-250 Lifecycle Annotations
10381040
[NOTE]
@@ -3617,9 +3619,29 @@ hierarchy runs within a transaction. Test methods that are not annotated with
36173619
`@Transactional` (at the class or method level) are not run within a transaction. Note
36183620
that `@Transactional` is not supported on test lifecycle methods — for example, methods
36193621
annotated with JUnit Jupiter's `@BeforeAll`, `@BeforeEach`, etc. Furthermore, tests that
3620-
are annotated with `@Transactional` but have the `propagation` type set to
3622+
are annotated with `@Transactional` but have the `propagation` attribute set to
36213623
`NOT_SUPPORTED` are not run within a transaction.
36223624

3625+
[[testcontext-tx-attribute-support]]
3626+
.`@Transactional` attribute support
3627+
|===
3628+
|Attribute |Supported for test-managed transactions
3629+
3630+
|`value` and `transactionManager` |yes
3631+
3632+
|`propagation` |only `Propagation.NOT_SUPPORTED` is supported
3633+
3634+
|`isolation` |no
3635+
3636+
|`timeout` |no
3637+
3638+
|`readOnly` |no
3639+
3640+
|`rollbackFor` and `rollbackForClassName` |no: use `TestTransaction.flagForRollback()` instead
3641+
3642+
|`noRollbackFor` and `noRollbackForClassName` |no: use `TestTransaction.flagForCommit()` instead
3643+
|===
3644+
36233645
[TIP]
36243646
====
36253647
Method-level lifecycle methods — for example, methods annotated with JUnit Jupiter's

0 commit comments

Comments
 (0)