Enforce REQUIRES_NEW for correct transaction configuration
Closes gh-31414
This commit is contained in:
parent
2cdc114273
commit
c7b832cfc8
|
@ -20,14 +20,12 @@ 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.scheduling.annotation.Async;
|
|
||||||
import org.springframework.transaction.event.TransactionalEventListenerFactory;
|
import org.springframework.transaction.event.TransactionalEventListenerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension of {@link TransactionalEventListenerFactory},
|
* Extension of {@link TransactionalEventListenerFactory},
|
||||||
* detecting invalid transaction configuration for transactional event listeners:
|
* detecting invalid transaction configuration for transactional event listeners:
|
||||||
* {@link Transactional} only supported with {@link Propagation#REQUIRES_NEW} or
|
* {@link Transactional} only supported with {@link Propagation#REQUIRES_NEW}.
|
||||||
* {@link Async}.
|
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @since 6.1
|
* @since 6.1
|
||||||
|
@ -39,10 +37,9 @@ 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) {
|
||||||
Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class);
|
Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class);
|
||||||
if (txAnn != null && txAnn.propagation() != Propagation.REQUIRES_NEW &&
|
if (txAnn != null && txAnn.propagation() != Propagation.REQUIRES_NEW) {
|
||||||
!AnnotatedElementUtils.hasAnnotation(method, Async.class)) {
|
|
||||||
throw new IllegalStateException("@TransactionalEventListener method must not be annotated with " +
|
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);
|
return super.createApplicationListener(beanName, type, method);
|
||||||
}
|
}
|
||||||
|
|
|
@ -233,7 +233,7 @@ public class TransactionalApplicationListenerMethodAdapterTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@TransactionalEventListener
|
@TransactionalEventListener
|
||||||
@Async @Transactional
|
@Async @Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||||
public void withAsyncTransactionalAnnotation(String data) {
|
public void withAsyncTransactionalAnnotation(String data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue