From c7b832cfc8721bb1c692d07cd5911f14070b58f5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 11 Oct 2023 17:14:32 +0200 Subject: [PATCH] Enforce REQUIRES_NEW for correct transaction configuration Closes gh-31414 --- .../RestrictedTransactionalEventListenerFactory.java | 9 +++------ ...ansactionalApplicationListenerMethodAdapterTests.java | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/RestrictedTransactionalEventListenerFactory.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/RestrictedTransactionalEventListenerFactory.java index 13f5d88e706..5d091505036 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/RestrictedTransactionalEventListenerFactory.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/RestrictedTransactionalEventListenerFactory.java @@ -20,14 +20,12 @@ import java.lang.reflect.Method; import org.springframework.context.ApplicationListener; import org.springframework.core.annotation.AnnotatedElementUtils; -import org.springframework.scheduling.annotation.Async; import org.springframework.transaction.event.TransactionalEventListenerFactory; /** * Extension of {@link TransactionalEventListenerFactory}, * detecting invalid transaction configuration for transactional event listeners: - * {@link Transactional} only supported with {@link Propagation#REQUIRES_NEW} or - * {@link Async}. + * {@link Transactional} only supported with {@link Propagation#REQUIRES_NEW}. * * @author Juergen Hoeller * @since 6.1 @@ -39,10 +37,9 @@ public class RestrictedTransactionalEventListenerFactory extends TransactionalEv @Override public ApplicationListener createApplicationListener(String beanName, Class type, Method method) { Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class); - if (txAnn != null && txAnn.propagation() != Propagation.REQUIRES_NEW && - !AnnotatedElementUtils.hasAnnotation(method, Async.class)) { + if (txAnn != null && txAnn.propagation() != Propagation.REQUIRES_NEW) { throw new IllegalStateException("@TransactionalEventListener method must not be annotated with " + - "@Transactional unless when marked as REQUIRES_NEW or declared as @Async: " + method); + "@Transactional unless when declared as REQUIRES_NEW: " + method); } return super.createApplicationListener(beanName, type, method); } diff --git a/spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapterTests.java b/spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapterTests.java index cfbefa4687c..84db8124ff1 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapterTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapterTests.java @@ -233,7 +233,7 @@ public class TransactionalApplicationListenerMethodAdapterTests { } @TransactionalEventListener - @Async @Transactional + @Async @Transactional(propagation = Propagation.REQUIRES_NEW) public void withAsyncTransactionalAnnotation(String data) { } }