MongoDB Reactive driver
Table of Contents
# Introduction
This sections covers the Mongock implementation for MongoDB Java Reactive Streams driver.
If you are curious about why mongock provides the mongodb-reactive-driver
, if it's synchronous by definition, please take a look to this faq entry
# Important note
Despite this deriver, the Mongock's nature is still synchronous: A migration consists in a list of changes, which are executed in order, one after the other. If one of them fails, the migration aborts and Mongock will take it from that point in the next execution.
For this reason...
Developers must block all the database calls inside a changeUnit
This is a bit cumbersome, as it requires creating a blocking subscriber that allows the developer to block the call.
Luckily, Mongock provides such a util class: MongoSubscriberSync. You can see an example here
Although Mongock provides this driver for those who, working in a reactive project, don't want to import the MongoDB java sync driver, we highly recommend the use of the synchronous drivers, when possible.
# MongoDB driver options and compatibility
Mongock driver | Driver library | Version compatibility |
---|---|---|
mongodb-reactive-driver | org.mongodb:mongodb-driver-reactivestreams | 4.X.X |
# MongoDB common configuration
All the MongoDB drivers share the same configuration.
When setting configuration via properties file, it must be prefixed by mongock.mongo-db
# Properties
Property | Description | Type | Default value |
---|---|---|---|
writeConcern | Exactly the same MongoDB parameter write concern. For more information, visit the official MongoDB documentation for write concern. | Object | {w:majority ,wTimeoutMs: null, j:true} |
readConcern | Exactly the same MongoDB parameter read concern. For more information, visit the official MongoDB documentation for read concern. | String | majority |
readPreference | Exactly the same MongoDB parameter read preference. For more information, visit the official MongoDB documentation for read preference. | String | primary |
# Get started
Following the get started section, this covers steps 3 and 5 and 6.
# - Add maven dependency for the driver (step 2)
<dependency> <groupId>io.mongock</groupId> <artifactId>mongodb-reactive-driver</artifactId></dependency>
# - Build the driver (step 5)
These classes provide the same two static initializers
- withDefaultLock(mongoClient, databaseName)
- withLockStrategy(mongoClient, databaseName, lockAcquiredForMillis, lockQuitTryingAfterMillis, lockTryFrequencyMillis)
// For mongodb-sync-v4-driverMongoReactiveDriver driver = MongoReactiveDriver.withDefaultLock(mongoClient, databaseName);// For mongodb-v3-driver//MongoCore3Driver driver = MongoCore3Driver.withDefaultLock(mongoClient, databaseName);driver.setWriteConcern(WriteConcern.MAJORITY.withJournal(true).withWTimeout(1000, TimeUnit.MILLISECONDS));driver.setReadConcern(ReadConcern.MAJORITY);driver.setReadPreference(ReadPreference.primary());
# - Driver extra configuration (step 6)
# Transactions
Due to the MongoDB API design, to work with transactions the ClientSession object is required in every MongoDB driver operation.
Mongock makes this very simple. The developer only needs to specify a ClientSession
parameter in the contructor or method of the @ChangeUnit
and use it in the MongoDB operations. Mongock takes care of everything else.
The following code shows how to save documents inside the transaction using the ClientSession
object.
@Execution public void execution(ClientSession clientSession, MongoDatabase mongoDatabase) { SubscriberSync<InsertManyResult> subscriber = new MongoSubscriberSync<>(); mongoDatabase.getCollection(CLIENTS_COLLECTION_NAME, Client.class) .insertMany(clientSession, IntStream.range(0, INITIAL_CLIENTS) .mapToObj(ClientInitializerChange::getClient) .collect(Collectors.toList())) .subscribe(subscriber); InsertManyResult result = subscriber.getFirst(); }
# Reactive with Spring Data
Please, take a look to this FAQ entry
# Examples
Please visit our example github repository for more information