Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Pavel
Pavel

Posted on • Edited on • Originally published atpakisan.github.io

     

Spring Boot 3 with MongoDB transactions

Transactions won't work without next things:

  • replica set

Since transactions are built on concepts of logical sessions they require mechanics (like oplog) which are only available in replica set environment.

You can always convert a standalone to a single noded replica set and transactions will work with this one node.

https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/

Reference

  • MongoTransactionManager

Unless you specify aMongoTransactionManager within your application context, transaction support is DISABLED. You can usesetSessionSynchronization(ALWAYS) to participate in ongoing non-native MongoDB transactions.

Reference

That's main reason why newly created Spring Boot project with MongoDB will fail with next error:

java.lang.IllegalStateException: Failed to retrieve PlatformTransactionManager for @Transactional test:
Enter fullscreen modeExit fullscreen mode

What to do:

  • Create replica set
  • ConfigureMongoTransactionManager asPlatformTransactionManager
  • Update application properties
  • Fix DataMongoTest tests

Create replica set

Choose preferred way to create replica set:

Configure PlatformTransactionManager

RegisterMongoTransactionManager in the application context.

importorg.jetbrains.annotations.NotNull;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.data.mongodb.MongoDatabaseFactory;importorg.springframework.data.mongodb.MongoTransactionManager;importorg.springframework.data.mongodb.config.AbstractMongoClientConfiguration;@ConfigurationpublicclassMongoClientConfigurationextendsAbstractMongoClientConfiguration{@Value("${spring.data.mongodb.database}")privateStringdatabaseName;@BeanpublicMongoTransactionManagertransactionManager(MongoDatabaseFactorymongoDatabaseFactory){returnnewMongoTransactionManager(mongoDatabaseFactory);}@NotNull@OverrideprotectedStringgetDatabaseName(){returndatabaseName;}}
Enter fullscreen modeExit fullscreen mode

Update application properties

spring.data.mongodb.replica-set-name=myReplicaSet
Enter fullscreen modeExit fullscreen mode

Fix DataMongoTest tests

Looks like@DataMongoTest still not activates autoconfiguration for MongoDB transactions.

References:

Proposal to importTransactionAutoConfiguration didn't help me

@ImportAutoConfiguration(TransactionAutoConfiguration.class)
Enter fullscreen modeExit fullscreen mode

So I decided to replace it with MongoDBConfig import

importapp.config.MongoDBConfig;importorg.junit.jupiter.api.Test;importorg.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;importorg.springframework.context.annotation.Import;importorg.springframework.transaction.annotation.Transactional;@DataMongoTest@Transactional@Import(MongoDBConfig.class)publicclassMongoTest{@TestpublicvoidtestTransaction(){}}
Enter fullscreen modeExit fullscreen mode

References

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp