Skip to content

Commit 646722b

Browse files
committed
GH-175 - Auto-configure transactions for MongoDB by default.
As the Event Publication registry only works in transactional environments, we should enable them for MongoDB by default. This will require a replica setup for the MongoDB instance the application interacts with.
1 parent 6dabfb4 commit 646722b

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2023 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.mongodb;
17+
18+
import org.springframework.boot.autoconfigure.AutoConfiguration;
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.data.mongodb.MongoDatabaseFactory;
22+
import org.springframework.data.mongodb.MongoTransactionManager;
23+
import org.springframework.modulith.events.EventPublicationRegistry;
24+
import org.springframework.transaction.PlatformTransactionManager;
25+
import org.springframework.transaction.support.TransactionTemplate;
26+
27+
/**
28+
* Auto-configuration to enable MongoDB transaction management as that is required for the
29+
* {@link EventPublicationRegistry} to work properly.
30+
*
31+
* @author Oliver Drotbohm
32+
*/
33+
@AutoConfiguration
34+
@ConditionalOnProperty(//
35+
name = "spring.modulith.events.mongobd.transaction-management.enabled",
36+
havingValue = "true",
37+
matchIfMissing = true)
38+
class MongoDbTransactionAutoConfiguration {
39+
40+
@Bean
41+
MongoTransactionManager transactionManager(MongoDatabaseFactory factory) {
42+
return new MongoTransactionManager(factory);
43+
}
44+
45+
@Bean
46+
TransactionTemplate transactionTemplate(PlatformTransactionManager txManager) {
47+
return new TransactionTemplate(txManager);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
org.springframework.modulith.events.mongodb.MongoDbEventPublicationAutoConfiguration
2+
org.springframework.modulith.events.mongodb.MongoDbTransactionAutoConfiguration

spring-modulith-events/spring-modulith-events-mongodb/src/test/java/org/springframework/modulith/events/mongodb/MongoDbEventPublicationAutoConfigurationIntegrationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.test.context.SpringBootTest;
2323
import org.springframework.context.ApplicationContext;
24+
import org.springframework.data.mongodb.MongoTransactionManager;
2425
import org.springframework.modulith.events.EventPublicationRegistry;
2526
import org.springframework.modulith.testapp.TestApplication;
2627

@@ -34,10 +35,11 @@ class MongoDbEventPublicationAutoConfigurationIntegrationTests {
3435

3536
@Autowired ApplicationContext context;
3637

37-
@Test // GH-4
38+
@Test // GH-4, GH-175
3839
void bootstrapsApplicationComponents() {
3940

4041
assertThat(context.getBean(EventPublicationRegistry.class)).isNotNull();
4142
assertThat(context.getBean(MongoDbEventPublicationRepository.class)).isNotNull();
43+
assertThat(context.getBean(MongoTransactionManager.class)).isNotNull();
4244
}
4345
}

src/docs/asciidoc/40-events.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,11 @@ To ease that task, Spring Modulith provides starter POMs that are centered aroun
147147
The following starters are available:
148148

149149
* `spring-modulith-starter-jpa` -- Using JPA as persistence technology.
150-
* `spring-modulith-starter-jdbc` -- Using JDBC as persistence technology. Also works in JPA-based applications but bypasses your JPA provider for actual event persistence.
150+
* `spring-modulith-starter-jdbc` -- Using JDBC as persistence technology.
151+
Also works in JPA-based applications but bypasses your JPA provider for actual event persistence.
151152
* `spring-modulith-starter-mongodb` -- Using MongoDB behind Spring Data MongoDB.
153+
Also enables MongoDB transactions and requires a replica set setup of the server to interact with.
154+
The transaction auto-configuration can be disabled by setting the `spring.modulith.events.mongobd.transaction-management.enabled` property to `false`.
152155

153156
[[events.integration-testing]]
154157
== Integration Testing Application Modules Working with Events

0 commit comments

Comments
 (0)