Skip to content

Commit 9d24996

Browse files
committed
GH-859 - Avoid creating a transaction for event externalization.
We now use the ability to override transaction propagation introduce for GH-858 to tweak the application module listener declaration on DelegatingEventListener so that we do not create a transaction by default. This avoids a database connection being acquired as the listener is very likely to interact with a non-transactional resource anyway.
1 parent f00e5dc commit 9d24996

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/support/DelegatingEventExternalizer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.modulith.events.EventExternalizationConfiguration;
2323
import org.springframework.modulith.events.RoutingTarget;
2424
import org.springframework.stereotype.Component;
25+
import org.springframework.transaction.annotation.Propagation;
2526
import org.springframework.util.Assert;
2627

2728
/**
@@ -59,7 +60,7 @@ public DelegatingEventExternalizer(EventExternalizationConfiguration configurati
5960
* @see org.springframework.modulith.events.support.EventExternalizationSupport#externalize(java.lang.Object)
6061
*/
6162
@Override
62-
@ApplicationModuleListener
63+
@ApplicationModuleListener(propagation = Propagation.SUPPORTS)
6364
public CompletableFuture<?> externalize(Object event) {
6465
return super.externalize(event);
6566
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.events.support;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.core.annotation.AnnotatedElementUtils;
22+
import org.springframework.transaction.annotation.Propagation;
23+
import org.springframework.transaction.annotation.Transactional;
24+
25+
/**
26+
* Unit tests for {@link DelegatingEventExternalizer}.
27+
*
28+
* @author Oliver Drotbohm
29+
* @since 1.3
30+
*/
31+
class DelegatingEventExternalizerUnitTests {
32+
33+
@Test // GH-859
34+
void doesNotRequireATransactionForExternalization() throws Exception {
35+
36+
var method = DelegatingEventExternalizer.class.getMethod("externalize", Object.class);
37+
var annotation = AnnotatedElementUtils.getMergedAnnotation(method, Transactional.class);
38+
39+
assertThat(annotation.propagation()).isEqualTo(Propagation.SUPPORTS);
40+
}
41+
}

0 commit comments

Comments
 (0)