Skip to content

Commit df13e4d

Browse files
committed
Document effect of preemptive timeouts on transactional tests
Issue: SPR-17647
1 parent bd9d333 commit df13e4d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/docs/asciidoc/testing.adoc

+23
Original file line numberDiff line numberDiff line change
@@ -3618,6 +3618,29 @@ caution if Spring-managed or application-managed transactions are configured wit
36183618
propagation type other than `REQUIRED` or `SUPPORTS` (see the discussion on
36193619
<<data-access.adoc#tx-propagation,transaction propagation>> for details).
36203620

3621+
.Preemptive timeouts and test-managed transactions
3622+
[WARNING]
3623+
====
3624+
Caution must be taken when using any form of preemptive timeouts from a testing framework
3625+
in conjunction with Spring's test-managed transactions.
3626+
3627+
Specifically, Spring’s testing support binds transaction state to the current thread (via
3628+
a `java.lang.ThreadLocal` variable) _before_ the current test method is invoked. If a
3629+
testing framework invokes the current test method in a new thread in order to support a
3630+
preemptive timeout, any actions performed within the current test method will _not_ be
3631+
invoked within the test-managed transaction. Consequently, the result of any such actions
3632+
will not be rolled back with the test-managed transaction. On the contrary, such actions
3633+
will be committed to the persistent store -- for example, a relational database -- even
3634+
though the test-managed transaction is properly rolled back by Spring.
3635+
3636+
Situations in which this can occur include but are not limited to the following.
3637+
3638+
* JUnit 4's `@Test(timeout = ...)` support and `TimeOut` rule
3639+
* JUnit Jupiter's `assertTimeoutPreemptively(...)` methods in the
3640+
`org.junit.jupiter.api.Assertions` class
3641+
* TestNG's `@Test(timeOut = ...)` support
3642+
====
3643+
36213644
[[testcontext-tx-enabling-transactions]]
36223645
===== Enabling and Disabling Transactions
36233646

0 commit comments

Comments
 (0)