Avoid transaction listener execution without transaction management setup

Closes gh-32319
This commit is contained in:
Juergen Hoeller
2024-02-23 11:53:05 +01:00
parent bed4d684e6
commit 524588ef93
5 changed files with 79 additions and 15 deletions

View File

@@ -269,8 +269,7 @@ class TransactionalEventListenerTests {
@Test
void noTransaction() {
load(BeforeCommitTestListener.class, AfterCompletionTestListener.class,
AfterCompletionExplicitTestListener.class);
load(BeforeCommitTestListener.class, AfterCompletionTestListener.class, AfterCompletionExplicitTestListener.class);
this.context.publishEvent("test");
getEventCollector().assertTotalEventsCount(0);
}
@@ -318,6 +317,24 @@ class TransactionalEventListenerTests {
getEventCollector().assertTotalEventsCount(4);
}
@Test
void noTransactionManagementWithFallbackExecution() {
doLoad(PlainConfiguration.class, FallbackExecutionTestListener.class);
this.context.publishEvent("test");
this.eventCollector.assertEvents(EventCollector.BEFORE_COMMIT, "test");
this.eventCollector.assertEvents(EventCollector.AFTER_COMMIT, "test");
this.eventCollector.assertEvents(EventCollector.AFTER_ROLLBACK, "test");
this.eventCollector.assertEvents(EventCollector.AFTER_COMPLETION, "test");
getEventCollector().assertTotalEventsCount(4);
}
@Test
void noTransactionManagementWithoutFallbackExecution() {
doLoad(PlainConfiguration.class, BeforeCommitTestListener.class, AfterCommitMetaAnnotationTestListener.class);
this.context.publishEvent("test");
this.eventCollector.assertNoEventReceived();
}
@Test
void conditionFoundOnTransactionalEventListener() {
load(ImmediateTestListener.class);
@@ -401,6 +418,31 @@ class TransactionalEventListenerTests {
}
@Configuration
static class PlainConfiguration {
@Bean
public EventCollector eventCollector() {
return new EventCollector();
}
@Bean
public TestBean testBean(ApplicationEventPublisher eventPublisher) {
return new TestBean(eventPublisher);
}
@Bean
public CallCountingTransactionManager transactionManager() {
return new CallCountingTransactionManager();
}
@Bean
public TransactionTemplate transactionTemplate() {
return new TransactionTemplate(transactionManager());
}
}
@Configuration
static class MulticasterWithCustomExecutor {