Skip to content

Commit

Permalink
Enforce REQUIRES_NEW for correct transaction configuration
Browse files Browse the repository at this point in the history
Closes gh-31414
  • Loading branch information
jhoeller committed Oct 11, 2023
1 parent 2cdc114 commit c7b832c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

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
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public void withTransactionalRequiresNewAnnotation(String data) {
}

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

0 comments on commit c7b832c

Please sign in to comment.