I'm new to morphia. I'm using morphia and mongo-java-driver.jar to talk with a replica-set (I need it for a cluster) through a Java program. I wrote the sample program below:
public static void createDBConnection() {
try {
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add( new ServerAddress( "192.168.1.80" , 27017 ) );
addrs.add( new ServerAddress( "192.168.1.81" , 27017 ) );
addrs.add( new ServerAddress( "192.168.1.82" , 27017 ) );
MorphiaObject.mongo = new MongoClient(addrs);
ReplicaSetStatus status = MorphiaObject.mongo.getReplicaSetStatus();
List<String> dbs = MorphiaObject.mongo.getDatabaseNames();
MongoOptions mongOptions = MorphiaObject.mongo.getMongoOptions();
MorphiaObject.mongo.setWriteConcern(WriteConcern.REPLICAS_SAFE);
MorphiaObject.mongo.setReadPreference(ReadPreference.secondaryPreferred());
System.out.println("Read prefrence"+ MorphiaObject.mongo.getReadPreference());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
- If I want to write data safely in all nodes, what are the necessary steps?
- What are the minimum configurations that I need to set for replica?
- What are validations needed to check if the replica set is down or alive, is readable, how can we manage if the app or program can't find the master?