Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Latest commit

 

History

History
52 lines (35 loc) · 1.84 KB

README.md

File metadata and controls

52 lines (35 loc) · 1.84 KB

robust-mongo-config

Example configuration of the Java MongoDB driver with Spring Data MongoDB to make the application more robust when a master slave switchover occures. The MongoTemplateWithRetry retries the most common database operations for three times. MongoDB specific exections will be catched and logged as warn. Only after the retry period the exeption will be thown as translated Spring database exception.

Requirements

For testing JRE 1.8 and Gradle is required.

For production use, just copy the MongoTemplateWithRetry.java and the MongoConfiguration.java into your project. Configure the MongoConfiguration file for your needs.

Starting a local MongoDB-Clusters for testing

Starting and initializing of three Mongo-Nodes:

mkdir -p /tmp/mongodb/rs0-1 /tmp/mongodb/rs0-2 /tmp/mongodb/rs0-3

mongod --port 27001 --replSet rs0 --smallfiles --oplogSize 128 --dbpath /tmp/mongodb/rs0-1
mongod --port 27002 --replSet rs0 --smallfiles --oplogSize 128 --dbpath /tmp/mongodb/rs0-2
mongod --port 27003 --replSet rs0 --smallfiles --oplogSize 128 --dbpath /tmp/mongodb/rs0-3

cat rs0.conf | mongo 127.0.0.1:27001

The ReplicaSet Configuration is saved in the file rs0.conf

Have a look at http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

Check configuration:

mongo 127.0.0.1:27001
rs.conf()
rs.status()

runing tests

src/test/java/de/hypoport/repository/JustARepositoryTest.java

When the test is running kill the primary Mongo node. The data should be written without break in case of unsteady replica set state. An information message should be logged.