2

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();

    }
}
  1. If I want to write data safely in all nodes, what are the necessary steps?
  2. What are the minimum configurations that I need to set for replica?
  3. 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?
THelper
  • 15,333
  • 6
  • 64
  • 104
Narendra
  • 151
  • 2
  • 12
  • Minimum? One server instance with the [`-replSet`](http://docs.mongodb.org/manual/reference/program/mongod/#cmdoption--replSet) option set, is basically considered a replica set, at least for all intensive purposes as far as the driver is concerned. At any rate, you "should" be using a [connection string](http://docs.mongodb.org/manual/reference/connection-string/) in your app. The "server list" is only a "seed list" as the driver will "self discover" all members involved. The point of the "seed list" is to provide options for connection, in case one of those servers is down on connect. – Blakes Seven Sep 07 '15 at 09:51
  • In a nutshell. Your app need not care. Just connect and let the replica set work it all out and inform the driver.If you want to know about "replica set configuration", then please ask on [dba.stackexchange.com](http://dba.stackexchange.com) instead. That is where all things "database" as opposed to "coding" are discussed. – Blakes Seven Sep 07 '15 at 09:54

0 Answers0