Skip to content

Commit

Permalink
Detect invalid transaction configuration for transactional event list…
Browse files Browse the repository at this point in the history
…eners

Closes gh-30679
  • Loading branch information
jhoeller committed Jun 17, 2023
1 parent 3568f6c commit 842569c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,6 +25,8 @@
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;

Expand Down Expand Up @@ -68,6 +70,11 @@ public TransactionalApplicationListenerMethodAdapter(String beanName, Class<?> t
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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 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.
Expand All @@ -24,12 +24,15 @@
import org.springframework.context.event.ApplicationListenerMethodAdapter;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;

/**
Expand Down Expand Up @@ -123,6 +126,19 @@ public void usesAnnotatedIdentifier() {
assertThat(adapter.getListenerId()).endsWith("identifier");
}

@Test
public void withTransactionalAnnotation() {
Method m = ReflectionUtils.findMethod(SampleEvents.class, "withTransactionalAnnotation", String.class);
assertThatIllegalStateException().isThrownBy(() -> createTestInstance(m));
}

@Test
public void withAsyncTransactionalAnnotation() {
Method m = ReflectionUtils.findMethod(SampleEvents.class, "withAsyncTransactionalAnnotation", String.class);
supportsEventType(true, m, createGenericEventType(String.class));
supportsEventType(false, m, createGenericEventType(Double.class));
}


private static void assertPhase(Method method, TransactionPhase expected) {
assertThat(method).as("Method must not be null").isNotNull();
Expand Down Expand Up @@ -194,6 +210,16 @@ public void throwing(String data) {
@TransactionalEventListener(id = "identifier")
public void identified(String data) {
}

@TransactionalEventListener
@Transactional
public void withTransactionalAnnotation(String data) {
}

@TransactionalEventListener
@Async @Transactional
public void withAsyncTransactionalAnnotation(String data) {
}
}

}

0 comments on commit 842569c

Please sign in to comment.