Skip to content

Commit c7b832c

Browse files
committed
Enforce REQUIRES_NEW for correct transaction configuration
Closes gh-31414
1 parent 2cdc114 commit c7b832c

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

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

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

2121
import org.springframework.context.ApplicationListener;
2222
import org.springframework.core.annotation.AnnotatedElementUtils;
23-
import org.springframework.scheduling.annotation.Async;
2423
import org.springframework.transaction.event.TransactionalEventListenerFactory;
2524

2625
/**
2726
* Extension of {@link TransactionalEventListenerFactory},
2827
* detecting invalid transaction configuration for transactional event listeners:
29-
* {@link Transactional} only supported with {@link Propagation#REQUIRES_NEW} or
30-
* {@link Async}.
28+
* {@link Transactional} only supported with {@link Propagation#REQUIRES_NEW}.
3129
*
3230
* @author Juergen Hoeller
3331
* @since 6.1
@@ -39,10 +37,9 @@ public class RestrictedTransactionalEventListenerFactory extends TransactionalEv
3937
@Override
4038
public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) {
4139
Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class);
42-
if (txAnn != null && txAnn.propagation() != Propagation.REQUIRES_NEW &&
43-
!AnnotatedElementUtils.hasAnnotation(method, Async.class)) {
40+
if (txAnn != null && txAnn.propagation() != Propagation.REQUIRES_NEW) {
4441
throw new IllegalStateException("@TransactionalEventListener method must not be annotated with " +
45-
"@Transactional unless when marked as REQUIRES_NEW or declared as @Async: " + method);
42+
"@Transactional unless when declared as REQUIRES_NEW: " + method);
4643
}
4744
return super.createApplicationListener(beanName, type, method);
4845
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public void withTransactionalRequiresNewAnnotation(String data) {
233233
}
234234

235235
@TransactionalEventListener
236-
@Async @Transactional
236+
@Async @Transactional(propagation = Propagation.REQUIRES_NEW)
237237
public void withAsyncTransactionalAnnotation(String data) {
238238
}
239239
}

0 commit comments

Comments
 (0)