Skip to content

Commit 8b74150

Browse files
committed
Explicit notes on transaction phase processing
Issue: SPR-15323
1 parent 9aaed2b commit 8b74150

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -98,6 +98,11 @@ public TransactionSynchronizationEventAdapter(ApplicationListenerMethodAdapter l
9898
this.phase = phase;
9999
}
100100

101+
@Override
102+
public int getOrder() {
103+
return this.listener.getOrder();
104+
}
105+
101106
@Override
102107
public void beforeCommit(boolean readOnly) {
103108
if (this.phase == TransactionPhase.BEFORE_COMMIT) {
@@ -107,22 +112,17 @@ public void beforeCommit(boolean readOnly) {
107112

108113
@Override
109114
public void afterCompletion(int status) {
110-
if (this.phase == TransactionPhase.AFTER_COMPLETION) {
115+
if (this.phase == TransactionPhase.AFTER_COMMIT && status == STATUS_COMMITTED) {
111116
processEvent();
112117
}
113-
else if (this.phase == TransactionPhase.AFTER_COMMIT && status == STATUS_COMMITTED) {
118+
else if (this.phase == TransactionPhase.AFTER_ROLLBACK && status == STATUS_ROLLED_BACK) {
114119
processEvent();
115120
}
116-
else if (this.phase == TransactionPhase.AFTER_ROLLBACK && status == STATUS_ROLLED_BACK) {
121+
else if (this.phase == TransactionPhase.AFTER_COMPLETION) {
117122
processEvent();
118123
}
119124
}
120125

121-
@Override
122-
public int getOrder() {
123-
return this.listener.getOrder();
124-
}
125-
126126
protected void processEvent() {
127127
this.listener.processEvent(this.event);
128128
}

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -22,6 +22,7 @@
2222
* The phase at which a transactional event listener applies.
2323
*
2424
* @author Stephane Nicoll
25+
* @author Juergen Hoeller
2526
* @since 4.2
2627
* @see TransactionalEventListener
2728
*/
@@ -33,27 +34,32 @@ public enum TransactionPhase {
3334
*/
3435
BEFORE_COMMIT,
3536

36-
/**
37-
* Fire the event after the transaction has completed. For
38-
* more fine-grained event, use {@link #AFTER_COMMIT} or
39-
* {@link #AFTER_ROLLBACK} to intercept transaction commit
40-
* or rollback respectively.
41-
* @see TransactionSynchronization#afterCompletion(int)
42-
*/
43-
AFTER_COMPLETION,
44-
4537
/**
4638
* 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,
41+
* (and not in {@link TransactionSynchronization#afterCommit()}).
4742
* @see TransactionSynchronization#afterCompletion(int)
4843
* @see TransactionSynchronization#STATUS_COMMITTED
4944
*/
5045
AFTER_COMMIT,
5146

5247
/**
5348
* 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.
5451
* @see TransactionSynchronization#afterCompletion(int)
5552
* @see TransactionSynchronization#STATUS_ROLLED_BACK
5653
*/
57-
AFTER_ROLLBACK
54+
AFTER_ROLLBACK,
55+
56+
/**
57+
* Fire the event after the transaction has completed.
58+
* <p>For more fine-grained events, use {@link #AFTER_COMMIT} or
59+
* {@link #AFTER_ROLLBACK} to intercept transaction commit
60+
* or rollback, respectively.
61+
* @see TransactionSynchronization#afterCompletion(int)
62+
*/
63+
AFTER_COMPLETION
5864

5965
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -28,25 +28,27 @@
2828
/**
2929
* An {@link EventListener} that is invoked according to a {@link TransactionPhase}.
3030
*
31-
* <p>If the event is not published within the boundaries of a managed transaction, the event
32-
* is discarded unless the {@link #fallbackExecution} flag is explicitly set. If a
31+
* <p>If the event is not published within the boundaries of a managed transaction, the
32+
* event is discarded unless the {@link #fallbackExecution} flag is explicitly set. If a
3333
* transaction is running, the event is processed according to its {@code TransactionPhase}.
3434
*
35-
* <p>Adding {@link org.springframework.core.annotation.Order @Order} on your annotated method
36-
* allows you to prioritize that listener amongst other listeners running in the same phase.
35+
* <p>Adding {@link org.springframework.core.annotation.Order @Order} to your annotated
36+
* method allows you to prioritize that listener amongst other listeners running before
37+
* or after transaction completion.
3738
*
3839
* @author Stephane Nicoll
3940
* @author Sam Brannen
4041
* @since 4.2
4142
*/
42-
@EventListener
4343
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
4444
@Retention(RetentionPolicy.RUNTIME)
4545
@Documented
46+
@EventListener
4647
public @interface TransactionalEventListener {
4748

4849
/**
4950
* Phase to bind the handling of an event to.
51+
* <p>The default phase is {@link TransactionPhase#AFTER_COMMIT}.
5052
* <p>If no transaction is in progress, the event is not processed at
5153
* all unless {@link #fallbackExecution} has been enabled explicitly.
5254
*/
@@ -76,7 +78,7 @@
7678
/**
7779
* Spring Expression Language (SpEL) attribute used for making the event
7880
* handling conditional.
79-
* <p>Default is {@code ""}, meaning the event is always handled.
81+
* <p>The default is {@code ""}, meaning the event is always handled.
8082
* @see EventListener#condition
8183
*/
8284
String condition() default "";

0 commit comments

Comments
 (0)