Allow any @Transactional propagation for listener with BEFORE_COMMIT phase
Backport Bot / build (push) Has been cancelled
Details
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Has been cancelled
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:17], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:23], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled
Details
Deploy Docs / Dispatch docs deployment (push) Has been cancelled
Details
Build and Deploy Snapshot / Verify (push) Has been cancelled
Details
Backport Bot / build (push) Has been cancelled
Details
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Has been cancelled
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:17], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:23], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled
Details
Deploy Docs / Dispatch docs deployment (push) Has been cancelled
Details
Build and Deploy Snapshot / Verify (push) Has been cancelled
Details
Closes gh-35150
This commit is contained in:
parent
67e88f3c20
commit
da13a24604
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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…
Reference in New Issue