Browse Source

Allow any @Transactional propagation for listener with BEFORE_COMMIT phase

Closes gh-35150
pull/35405/head
Juergen Hoeller 5 months ago
parent
commit
da13a24604
  1. 10
      spring-tx/src/main/java/org/springframework/transaction/annotation/RestrictedTransactionalEventListenerFactory.java
  2. 12
      spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapterTests.java

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

@ -20,6 +20,8 @@ import java.lang.reflect.Method;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalApplicationListenerMethodAdapter;
import org.springframework.transaction.event.TransactionalEventListenerFactory; import org.springframework.transaction.event.TransactionalEventListenerFactory;
/** /**
@ -37,12 +39,13 @@ public class RestrictedTransactionalEventListenerFactory extends TransactionalEv
@Override @Override
public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) { public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) {
TransactionalApplicationListenerMethodAdapter adapter =
new TransactionalApplicationListenerMethodAdapter(beanName, type, method);
if (adapter.getTransactionPhase() != TransactionPhase.BEFORE_COMMIT) {
Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class); Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class);
if (txAnn == null) { if (txAnn == null) {
txAnn = AnnotatedElementUtils.findMergedAnnotation(type, Transactional.class); txAnn = AnnotatedElementUtils.findMergedAnnotation(type, Transactional.class);
} }
if (txAnn != null) { if (txAnn != null) {
Propagation propagation = txAnn.propagation(); Propagation propagation = txAnn.propagation();
if (propagation != Propagation.REQUIRES_NEW && propagation != Propagation.NOT_SUPPORTED) { if (propagation != Propagation.REQUIRES_NEW && propagation != Propagation.NOT_SUPPORTED) {
@ -50,7 +53,8 @@ public class RestrictedTransactionalEventListenerFactory extends TransactionalEv
"@Transactional unless when declared as REQUIRES_NEW or NOT_SUPPORTED: " + method); "@Transactional unless when declared as REQUIRES_NEW or NOT_SUPPORTED: " + method);
} }
} }
return super.createApplicationListener(beanName, type, method); }
return adapter;
} }
} }

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

@ -157,6 +157,13 @@ class TransactionalApplicationListenerMethodAdapterTests {
assertThatNoException().isThrownBy(() -> factory.createApplicationListener("test", SampleEvents.class, m)); assertThatNoException().isThrownBy(() -> factory.createApplicationListener("test", SampleEvents.class, m));
} }
@Test
void withTransactionalAnnotationBeforeCommit() {
RestrictedTransactionalEventListenerFactory factory = new RestrictedTransactionalEventListenerFactory();
Method m = ReflectionUtils.findMethod(SampleEvents.class, "withTransactionalAnnotationBeforeCommit", String.class);
assertThatNoException().isThrownBy(() -> factory.createApplicationListener("test", SampleEvents.class, m));
}
@Test @Test
void withTransactionalAnnotationOnEnclosingClass() { void withTransactionalAnnotationOnEnclosingClass() {
RestrictedTransactionalEventListenerFactory factory = new RestrictedTransactionalEventListenerFactory(); RestrictedTransactionalEventListenerFactory factory = new RestrictedTransactionalEventListenerFactory();
@ -277,6 +284,11 @@ class TransactionalApplicationListenerMethodAdapterTests {
public void withAsyncTransactionalAnnotation(String data) { public void withAsyncTransactionalAnnotation(String data) {
} }
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
@Transactional
public void withTransactionalAnnotationBeforeCommit(String data) {
}
@Transactional @Transactional
static class SampleEventsWithTransactionalAnnotation { static class SampleEventsWithTransactionalAnnotation {

Loading…
Cancel
Save