Example usage for com.mongodb WriteConcern SAFE

List of usage examples for com.mongodb WriteConcern SAFE

Introduction

In this page you can find the example usage for com.mongodb WriteConcern SAFE.

Prototype

WriteConcern SAFE

To view the source code for com.mongodb WriteConcern SAFE.

Click Source Link

Document

Write operations that use this write concern will wait for acknowledgement from the primary server before returning.

Usage

From source file:com.ebay.jetstream.configurationmanagement.MongoLogDAO.java

License:MIT License

public static boolean removeConfigurationByQuery(BasicDBObject query, MongoLogConnection mongoLogConnection) {

    DBCollection dbCol = mongoLogConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
    }/*from  w ww  . j a  v  a 2 s  .co m*/

    try {
        if (query == null) {
            return false;
        }

        WriteResult result = dbCol.remove(query, WriteConcern.SAFE);

        if (result.getLastError().ok()) {
            return true;
        }

    } catch (Exception err) {
        throw new MongoConfigRuntimeException(err);
    }

    return true;
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void update(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.upQuery)).getDBObject();
    final BasicDBObject update = (BasicDBObject) ((DocBuilderField) getBoundUnit(Item.upUpdate)).getDBObject();
    final boolean upsert = getBooleanFieldValue(Item.upUpsert);
    final boolean multi = getBooleanFieldValue(Item.upMulti);
    final boolean safe = getBooleanFieldValue(Item.upSafe);
    col.setWriteConcern(WriteConcern.SAFE);

    new DbJob() {
        @Override//ww  w . jav  a 2 s.  c  om
        public Object doRun() {
            if (safe) {
                long count = col.getCount(query);
                long toupdate = count > 0 ? 1 : 0;
                if (multi) {
                    toupdate = count;
                }
                String text = "Proceed with updating " + toupdate + " of " + count + " documents?";
                ConfirmDialog confirm = new ConfirmDialog(null, "Confirm Update", null, text);
                if (!confirm.show()) {
                    return null;
                }
            }
            return col.update(query, (DBObject) update.copy(), upsert, multi);
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Update";
        }

        @Override
        public DBObject getRoot(Object result) {
            BasicDBObject obj = new BasicDBObject("query", query);
            obj.put("update", update);
            obj.put("upsert", upsert);
            obj.put("multi", multi);
            return obj;
        }

        @Override
        public ButtonBase getButton() {
            return button;
        }
    }.addJob();
}

From source file:com.faujnet.mongo.repository.UserSocialConnectionService.java

License:Apache License

/**
 * Update a connection.//from w ww .j  av  a  2 s  .co  m
 * 
 * @see org.springframework.social.connect.mongo.ConnectionService#update(java.lang.String, org.springframework.social.connect.Connection)
 */
@Override
public void update(String userId, Connection<?> userConn) {
    UserSocialConnection mongoCnn = converter.convert(userConn);
    mongoCnn.setUserId(userId);
    try {
        mongoTemplate.setWriteConcern(WriteConcern.SAFE);
        mongoTemplate.save(mongoCnn);
    } catch (DuplicateKeyException e) {
        Query q = query(where("userId").is(userId).and("providerId").is(mongoCnn.getProviderId())
                .and("providerUserId").is(mongoCnn.getProviderUserId()));

        Update update = Update.update("expireTime", mongoCnn.getExpireTime())
                .set("accessToken", mongoCnn.getAccessToken()).set("profileUrl", mongoCnn.getProfileUrl())
                .set("imageUrl", mongoCnn.getImageUrl()).set("displayName", mongoCnn.getDisplayName());

        mongoTemplate.findAndModify(q, update, UserSocialConnection.class);
    }
}

From source file:com.flyingdonut.implementation.helpers.MongoDbConsumerAssociationStore.java

License:Apache License

@Override
public void save(String opUrl, Association association) {
    removeExpired();/* w w w .  java2  s. co  m*/
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put(opurlField, opUrl);
    dbObject.put(handleField, association.getHandle());
    dbObject.put(typeField, association.getType());
    dbObject.put(mackeyField, association.getMacKey() == null ? null
            : new String(Base64.encodeBase64(association.getMacKey().getEncoded())));
    dbObject.put(expdateField, association.getExpiry());
    String collection = getCollectionName();
    WriteResult writeResult = getMongoDBConnection().getCollection(collection).insert(dbObject,
            WriteConcern.SAFE);
}

From source file:com.github.carlomicieli.nerdmovies.config.ApplicationConfig.java

License:Apache License

/**
 * Return the MongoDB template.//from  w  w w  .j ava  2s  .co  m
 *
 * @return the MongoDB template bean.
 * @throws Exception
 */
@Bean
public MongoTemplate mongoTemplate() throws Exception {
    MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
    mongoTemplate.setWriteConcern(WriteConcern.SAFE);
    return mongoTemplate;
}

From source file:com.github.mongo.labs.Main.java

License:Apache License

public static void initMongoProfiling() {
    try {/*w ww  .j  av a2s  .  co m*/
        // bye bye Mongo driver logs
        Logger.getLogger("com.mongodb").setLevel(Level.OFF);

        DB db = new MongoClient("localhost").getDB("devoxx");
        Jongo jongo = new Jongo(db);

        ResultCmd r = jongo.getCollection("$cmd").withWriteConcern(WriteConcern.SAFE).findOne("{profile: 2}")
                .as(ResultCmd.class);// log event fast query

        System.err.println("profiling =" + r);
    } catch (IOException e) {
        throwMongoNotStarted();
    } catch (MongoException e) {
        throwMongoNotStarted();
    }
}

From source file:com.google.api.ads.adwords.awreporting.model.persistence.mongodb.MongoEntityPersister.java

License:Open Source License

@Override
public <T> T save(T t) {
    T newT = null;//ww  w.  j a  v  a 2s .  c o  m
    if (t != null) {
        String jsonObject = gson.toJson(t);
        DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(jsonObject.toString());
        dbObject.put("safe", "true");

        // Set the proper _id from the MongoEntity RowID
        if (t instanceof MongoEntity) {
            dbObject.put("_id", ((MongoEntity) t).getRowId());
        }

        getCollection(t.getClass()).save(dbObject, WriteConcern.SAFE);
    }
    return newT;
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.mongodb.MongoEntityPersister.java

License:Open Source License

/**
 * @see com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.EntityPersister
 *      #save(java.lang.Object)/*www .j a v  a  2 s .  c  o m*/
 */
@Override
public <T> T save(T t) {
    T newT = null;
    if (t != null) {

        DBCollection dbcollection = getDBCollection(t.getClass(), true);

        String jsonObject;
        jsonObject = gson.toJson(t);
        DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(jsonObject.toString());
        dbObject.put("safe", "true");
        dbcollection.save(dbObject, WriteConcern.SAFE);
    }
    return newT;
}

From source file:com.hydadmin.utilities.SpringMongoConfig.java

public @Bean MongoTemplate mongoTemplate() throws Exception {

    MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());

    // show error, should off on production to speed up performance
    mongoTemplate.setWriteConcern(WriteConcern.SAFE);

    return mongoTemplate;

}

From source file:com.ibm.research.mongotx.lrc.LatestReadCommittedTxDB.java

License:Open Source License

public LatestReadCommittedTxDB(MongoClient client, MongoDatabase db) {
    this.client = client;
    this.db = db;
    this.db.withWriteConcern(WriteConcern.SAFE);
    if (db.getCollection(COL_SYSTEM) == null) {
        db.createCollection(COL_SYSTEM);
        this.sysCol = new MongoProfilingCollection(db.getCollection(COL_SYSTEM));
        this.sysCol.createIndex(new Document(ATTR_TX_TIMEOUT, true));
        this.sysCol.createIndex(new Document(ATTR_TX_STARTTIME, true));
    } else {//ww  w  .  j  a  va  2 s  .  c om
        this.sysCol = new MongoProfilingCollection(db.getCollection(COL_SYSTEM));
    }
    this.clientId = incrementAndGetLong(ID_CLIENT);
    this.isSharding = isSharding();

    getCurrentTimeInServer();

    long requestTs = System.currentTimeMillis();
    long serverTs = getCurrentTimeInServer();
    long responseTs = System.currentTimeMillis();

    this.timeGapMin = requestTs - serverTs - MAX_TIMEDIFF;
    this.timeGapMax = responseTs - serverTs + MAX_TIMEDIFF;
}