Detect invalid transaction configuration for transactional event listeners

Closes gh-30679
This commit is contained in:
Juergen Hoeller
2023-06-17 16:13:59 +02:00
parent 3568f6c61a
commit 842569c9e5
2 changed files with 35 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,8 @@ import org.springframework.context.event.ApplicationListenerMethodAdapter;
import org.springframework.context.event.EventListener;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
@@ -68,6 +70,11 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi
if (ann == null) {
throw new IllegalStateException("No TransactionalEventListener annotation found on method: " + method);
}
if (AnnotatedElementUtils.hasAnnotation(method, Transactional.class) &&
!AnnotatedElementUtils.hasAnnotation(method, Async.class)) {
throw new IllegalStateException("@TransactionalEventListener method must not be annotated " +
"with @Transactional, unless when declared as @Async: " + method);
}
this.annotation = ann;
this.transactionPhase = ann.phase();
}