Skip to content

Commit 911aca1

Browse files
committed
Doc tx semantics for @TransactionalEventListener after completion phases
This commit improves the Javadoc regarding transactional semantics for @TransactionalEventListener methods invoked in the AFTER_COMMIT, AFTER_ROLLBACK, or AFTER_COMPLETION phase. Specifically, the documentation now points out that interactions with the underlying transactional resource will not be committed in those phases. Closes gh-26974
1 parent 9018356 commit 911aca1

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

spring-context/src/main/java/org/springframework/context/event/EventListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
* @author Sam Brannen
8484
* @since 4.2
8585
* @see EventListenerMethodProcessor
86+
* @see org.springframework.transaction.event.TransactionalEventListener
8687
*/
8788
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
8889
@Retention(RetentionPolicy.RUNTIME)

spring-tx/src/main/java/org/springframework/transaction/event/TransactionPhase.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,45 +19,55 @@
1919
import org.springframework.transaction.support.TransactionSynchronization;
2020

2121
/**
22-
* The phase at which a transactional event listener applies.
22+
* The phase in which a transactional event listener applies.
2323
*
2424
* @author Stephane Nicoll
2525
* @author Juergen Hoeller
26+
* @author Sam Brannen
2627
* @since 4.2
2728
* @see TransactionalEventListener
2829
*/
2930
public enum TransactionPhase {
3031

3132
/**
32-
* Fire the event before transaction commit.
33+
* Handle the event before transaction commit.
3334
* @see TransactionSynchronization#beforeCommit(boolean)
3435
*/
3536
BEFORE_COMMIT,
3637

3738
/**
38-
* Fire the event after the commit has completed successfully.
39-
* <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and
40-
* therefore executes in the same after-completion sequence of events,
39+
* Handle the event after the commit has completed successfully.
40+
* <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and therefore
41+
* executes in the same sequence of events as {@code AFTER_COMPLETION}
4142
* (and not in {@link TransactionSynchronization#afterCommit()}).
43+
* <p>Interactions with the underlying transactional resource will not be
44+
* committed in this phase. See
45+
* {@link TransactionSynchronization#afterCompletion(int)} for details.
4246
* @see TransactionSynchronization#afterCompletion(int)
4347
* @see TransactionSynchronization#STATUS_COMMITTED
4448
*/
4549
AFTER_COMMIT,
4650

4751
/**
48-
* Fire the event if the transaction has rolled back.
49-
* <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and
50-
* therefore executes in the same after-completion sequence of events.
52+
* Handle the event if the transaction has rolled back.
53+
* <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and therefore
54+
* executes in the same sequence of events as {@code AFTER_COMPLETION}.
55+
* <p>Interactions with the underlying transactional resource will not be
56+
* committed in this phase. See
57+
* {@link TransactionSynchronization#afterCompletion(int)} for details.
5158
* @see TransactionSynchronization#afterCompletion(int)
5259
* @see TransactionSynchronization#STATUS_ROLLED_BACK
5360
*/
5461
AFTER_ROLLBACK,
5562

5663
/**
57-
* Fire the event after the transaction has completed.
64+
* Handle the event after the transaction has completed.
5865
* <p>For more fine-grained events, use {@link #AFTER_COMMIT} or
5966
* {@link #AFTER_ROLLBACK} to intercept transaction commit
6067
* or rollback, respectively.
68+
* <p>Interactions with the underlying transactional resource will not be
69+
* committed in this phase. See
70+
* {@link TransactionSynchronization#afterCompletion(int)} for details.
6171
* @see TransactionSynchronization#afterCompletion(int)
6272
*/
6373
AFTER_COMPLETION

spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListener.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,18 +30,30 @@
3030
*
3131
* <p>If the event is not published within an active transaction, the event is discarded
3232
* unless the {@link #fallbackExecution} flag is explicitly set. If a transaction is
33-
* running, the event is processed according to its {@code TransactionPhase}.
33+
* running, the event is handled according to its {@code TransactionPhase}.
3434
*
3535
* <p>Adding {@link org.springframework.core.annotation.Order @Order} to your annotated
3636
* method allows you to prioritize that listener amongst other listeners running before
3737
* or after transaction completion.
3838
*
3939
* <p><b>NOTE: Transactional event listeners only work with thread-bound transactions
40-
* managed by {@link org.springframework.transaction.PlatformTransactionManager}.</b>
41-
* A reactive transaction managed by {@link org.springframework.transaction.ReactiveTransactionManager}
42-
* uses the Reactor context instead of thread-local attributes, so from the perspective of
40+
* managed by a {@link org.springframework.transaction.PlatformTransactionManager
41+
* PlatformTransactionManager}.</b> A reactive transaction managed by a
42+
* {@link org.springframework.transaction.ReactiveTransactionManager ReactiveTransactionManager}
43+
* uses the Reactor context instead of thread-local variables, so from the perspective of
4344
* an event listener, there is no compatible active transaction that it can participate in.
4445
*
46+
* <p><strong>WARNING:</strong> if the {@code TransactionPhase} is set to
47+
* {@link TransactionPhase#AFTER_COMMIT AFTER_COMMIT} (the default),
48+
* {@link TransactionPhase#AFTER_ROLLBACK AFTER_ROLLBACK}, or
49+
* {@link TransactionPhase#AFTER_COMPLETION AFTER_COMPLETION}, the transaction will
50+
* have been committed or rolled back already, but the transactional resources might
51+
* still be active and accessible. As a consequence, any data access code triggered
52+
* at this point will still "participate" in the original transaction, but changes
53+
* will not be committed to the transactional resource. See
54+
* {@link org.springframework.transaction.support.TransactionSynchronization#afterCompletion(int)
55+
* TransactionSynchronization.afterCompletion(int)} for details.
56+
*
4557
* @author Stephane Nicoll
4658
* @author Sam Brannen
4759
* @since 4.2
@@ -61,7 +73,7 @@
6173
TransactionPhase phase() default TransactionPhase.AFTER_COMMIT;
6274

6375
/**
64-
* Whether the event should be processed if no transaction is running.
76+
* Whether the event should be handled if no transaction is running.
6577
*/
6678
boolean fallbackExecution() default false;
6779

0 commit comments

Comments
 (0)