private void setWriteReadOptionsOnConnection() { // Set the WriteConcern if (writeConcern != null) { mongoConnection.setWriteConcern(writeConcern); } else if (writeConcernRef != null) { mongoConnection.setWriteConcern(writeConcernRef); } // Set the ReadPreference if (readPreference != null) { mongoConnection.setReadPreference(readPreference); } }
mongo.setReadPreference(ReadPreference.secondaryPreferred()); mongo.setWriteConcern(WriteConcern.ACKNOWLEDGED); return mongo;
if (readPreferenceState != null) { com.mongodb.ReadPreference readPreference = new ReadPreferenceState(this, readPreferenceState).getReadPreference(); mongoClient.setReadPreference(readPreference);
public MongoConfig(DataService dataService, String configId, Map<String, String> properties, boolean odataEnable) throws DataServiceFault { super(dataService, configId, DBConstants.DataSourceTypes.MONGODB, properties, odataEnable); String serversParam = properties.get(DBConstants.MongoDB.SERVERS); if (DBUtils.isEmptyString(serversParam)) { throw new DataServiceFault("The data source param '" + DBConstants.MongoDB.SERVERS + "' is required"); } this.servers = serversParam.split(","); String database = properties.get(DBConstants.MongoDB.DATABASE); if (DBUtils.isEmptyString(database)) { throw new DataServiceFault("The data source param '" + DBConstants.MongoDB.DATABASE + "' is required"); } try { this.mongoClientOptions = extractMongoOptions(properties); this.mongoClient = createNewMongo(properties); String writeConcern = properties.get(DBConstants.MongoDB.WRITE_CONCERN); if (!DBUtils.isEmptyString(writeConcern)) { this.getMongoClient().setWriteConcern(WriteConcern.valueOf(writeConcern)); } String readPref = properties.get(DBConstants.MongoDB.READ_PREFERENCE); if (!DBUtils.isEmptyString(readPref)) { this.getMongoClient().setReadPreference(ReadPreference.valueOf(readPref)); } this.getMongoClient().getDatabase(database); this.jongo = new Jongo(this.getMongoClient().getDB(database)); } catch (Exception e) { throw new DataServiceFault(e, DBConstants.FaultCodes.CONNECTION_UNAVAILABLE_ERROR, e.getMessage()); } }
/** @return The {@link DBObject} representing the object with the given id */ public static DBObject getById(AbstractMongoDBServer entity, String id) { LOG.info("Getting {} from {}", new Object[]{id, entity}); MongoClient mongoClient = clientForServer(entity); // Secondary preferred means the driver will let us read from secondaries too. mongoClient.setReadPreference(ReadPreference.secondaryPreferred()); try { DB db = mongoClient.getDB(TEST_DB); DBCollection testCollection = db.getCollection(TEST_COLLECTION); return testCollection.findOne(new BasicDBObject("_id", new ObjectId(id))); } finally { mongoClient.close(); } }