Skip to content

Commit

Permalink
Allow Propagation.NOT_SUPPORTED with @TransactionalEventListener
Browse files Browse the repository at this point in the history
Closes gh-31907
  • Loading branch information
jhoeller committed Dec 26, 2023
1 parent 17d362f commit 75c7596
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
/**
* Extension of {@link TransactionalEventListenerFactory},
* detecting invalid transaction configuration for transactional event listeners:
* {@link Transactional} only supported with {@link Propagation#REQUIRES_NEW}.
* {@link Transactional} only supported with {@link Propagation#REQUIRES_NEW}
* and {@link Propagation#NEVER}.

This comment has been minimized.

Copy link
@xak2000

xak2000 Dec 26, 2023

Contributor

Should be Propagation.NOT_SUPPORTED in the javadoc to correspond to the imlementation.

*
* @author Juergen Hoeller
* @since 6.1
Expand All @@ -37,9 +38,12 @@ 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) {
throw new IllegalStateException("@TransactionalEventListener method must not be annotated with " +
"@Transactional unless when declared as REQUIRES_NEW: " + method);
if (txAnn != null) {
Propagation propagation = txAnn.propagation();
if (propagation != Propagation.REQUIRES_NEW && propagation != Propagation.NOT_SUPPORTED) {
throw new IllegalStateException("@TransactionalEventListener method must not be annotated with " +
"@Transactional unless when declared as REQUIRES_NEW or NOT_SUPPORTED: " + method);
}
}
return super.createApplicationListener(beanName, type, method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public void withTransactionalRequiresNewAnnotation() {
assertThatNoException().isThrownBy(() -> factory.createApplicationListener("test", SampleEvents.class, m));
}

@Test
public void withTransactionalNotSupportedAnnotation() {
RestrictedTransactionalEventListenerFactory factory = new RestrictedTransactionalEventListenerFactory();
Method m = ReflectionUtils.findMethod(SampleEvents.class, "withTransactionalNotSupportedAnnotation", String.class);
assertThatNoException().isThrownBy(() -> factory.createApplicationListener("test", SampleEvents.class, m));
}

@Test
public void withAsyncTransactionalAnnotation() {
RestrictedTransactionalEventListenerFactory factory = new RestrictedTransactionalEventListenerFactory();
Expand Down Expand Up @@ -232,6 +239,11 @@ public void withTransactionalAnnotation(String data) {
public void withTransactionalRequiresNewAnnotation(String data) {
}

@TransactionalEventListener
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void withTransactionalNotSupportedAnnotation(String data) {
}

@TransactionalEventListener
@Async @Transactional(propagation = Propagation.REQUIRES_NEW)
public void withAsyncTransactionalAnnotation(String data) {
Expand Down

0 comments on commit 75c7596

Please sign in to comment.