Skip to content

Commit da13a24

Browse files
committed
Allow any @transactional propagation for listener with BEFORE_COMMIT phase
Closes gh-35150
1 parent 67e88f3 commit da13a24

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

spring-tx/src/main/java/org/springframework/transaction/annotation/RestrictedTransactionalEventListenerFactory.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.springframework.context.ApplicationListener;
2222
import org.springframework.core.annotation.AnnotatedElementUtils;
23+
import org.springframework.transaction.event.TransactionPhase;
24+
import org.springframework.transaction.event.TransactionalApplicationListenerMethodAdapter;
2325
import org.springframework.transaction.event.TransactionalEventListenerFactory;
2426

2527
/**
@@ -37,20 +39,22 @@ public class RestrictedTransactionalEventListenerFactory extends TransactionalEv
3739

3840
@Override
3941
public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) {
40-
Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class);
41-
42-
if (txAnn == null) {
43-
txAnn = AnnotatedElementUtils.findMergedAnnotation(type, Transactional.class);
44-
}
45-
46-
if (txAnn != null) {
47-
Propagation propagation = txAnn.propagation();
48-
if (propagation != Propagation.REQUIRES_NEW && propagation != Propagation.NOT_SUPPORTED) {
49-
throw new IllegalStateException("@TransactionalEventListener method must not be annotated with " +
50-
"@Transactional unless when declared as REQUIRES_NEW or NOT_SUPPORTED: " + method);
42+
TransactionalApplicationListenerMethodAdapter adapter =
43+
new TransactionalApplicationListenerMethodAdapter(beanName, type, method);
44+
if (adapter.getTransactionPhase() != TransactionPhase.BEFORE_COMMIT) {
45+
Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class);
46+
if (txAnn == null) {
47+
txAnn = AnnotatedElementUtils.findMergedAnnotation(type, Transactional.class);
48+
}
49+
if (txAnn != null) {
50+
Propagation propagation = txAnn.propagation();
51+
if (propagation != Propagation.REQUIRES_NEW && propagation != Propagation.NOT_SUPPORTED) {
52+
throw new IllegalStateException("@TransactionalEventListener method must not be annotated with " +
53+
"@Transactional unless when declared as REQUIRES_NEW or NOT_SUPPORTED: " + method);
54+
}
5155
}
5256
}
53-
return super.createApplicationListener(beanName, type, method);
57+
return adapter;
5458
}
5559

5660
}

spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapterTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ void withAsyncTransactionalAnnotation() {
157157
assertThatNoException().isThrownBy(() -> factory.createApplicationListener("test", SampleEvents.class, m));
158158
}
159159

160+
@Test
161+
void withTransactionalAnnotationBeforeCommit() {
162+
RestrictedTransactionalEventListenerFactory factory = new RestrictedTransactionalEventListenerFactory();
163+
Method m = ReflectionUtils.findMethod(SampleEvents.class, "withTransactionalAnnotationBeforeCommit", String.class);
164+
assertThatNoException().isThrownBy(() -> factory.createApplicationListener("test", SampleEvents.class, m));
165+
}
166+
160167
@Test
161168
void withTransactionalAnnotationOnEnclosingClass() {
162169
RestrictedTransactionalEventListenerFactory factory = new RestrictedTransactionalEventListenerFactory();
@@ -277,6 +284,11 @@ public void withTransactionalNotSupportedAnnotation(String data) {
277284
public void withAsyncTransactionalAnnotation(String data) {
278285
}
279286

287+
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
288+
@Transactional
289+
public void withTransactionalAnnotationBeforeCommit(String data) {
290+
}
291+
280292
@Transactional
281293
static class SampleEventsWithTransactionalAnnotation {
282294

0 commit comments

Comments
 (0)