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:fr.xebia.cocktail.CocktailRepository.java

License:Apache License

public boolean delete(Cocktail cocktail) {
    Preconditions.checkNotNull(cocktail.getObjectId(), "Given objectId must not be null in %s", cocktail);
    try {/*w ww.j a va2 s . c o m*/
        // SOLR
        UpdateResponse solrResponse = solrServer.deleteByQuery("id:" + cocktail.getId());
        logger.trace("solr.delete for {}: {}", cocktail, solrResponse);

        // MONGODB
        WriteResult mongoResult = cocktails.remove(
                BasicDBObjectBuilder.start().add("_id", cocktail.getObjectId()).get(), WriteConcern.SAFE);
        logger.trace("mongo.remove for {}: {}", cocktail, mongoResult);

        return mongoResult.getN() > 0;
    } catch (Exception e) {
        throw new RuntimeException("Exception deleting " + cocktail, e);
    }
}

From source file:fr.xebia.cocktail.CocktailRepository.java

License:Apache License

public void insert(Cocktail cocktail) {
    Preconditions.checkArgument(cocktail.getObjectId() == null, "Given objectId must be null in %s", cocktail);
    try {//from   w  w  w  .j av a 2  s  . c  om
        cocktail.setObjectId(ObjectId.get());

        // SOLR
        SolrInputDocument solrInputDocument = toSolrInputDocument(cocktail);
        UpdateRequest req = new UpdateRequest();
        req.setAction(org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION.COMMIT, false, false);
        req.add(solrInputDocument);
        UpdateResponse solrResponse = req.process(solrServer);
        logger.trace("solr.add for {}: {}", cocktail, solrResponse);

        // MONGODB
        DBObject bson = COCKTAIL_TO_BSON.apply(cocktail);
        WriteResult mongoResult = cocktails.insert(bson, WriteConcern.SAFE);
        logger.trace("mongo.insert for {}: {}", cocktail, mongoResult);
    } catch (Exception e) {
        throw new RuntimeException("Exception updating " + cocktail, e);
    }

}

From source file:fr.xebia.cocktail.CocktailRepository.java

License:Apache License

public void purgeRepository() {
    try {//from  ww  w . j a  v  a2 s  . c  o m
        // SOLR
        UpdateResponse solrResponse = solrServer.deleteByQuery("*:*");
        logger.trace("solr.delete all: {}", solrResponse);

        // MONGODB
        WriteResult mongoResult = cocktails.remove(BasicDBObjectBuilder.start().get(), WriteConcern.SAFE);
        logger.trace("mongo.remove all", mongoResult);
    } catch (Exception e) {
        throw new RuntimeException("Exception purging repository", e);
    }
}

From source file:fr.xebia.cocktail.CocktailRepository.java

License:Apache License

public void update(Cocktail cocktail) {
    Preconditions.checkNotNull(cocktail.getObjectId(), "Given objectId must not be null in %s", cocktail);
    try {/*w  ww . j a  va2  s. c  om*/

        // SOLR
        SolrInputDocument solrInputDocument = toSolrInputDocument(cocktail);
        UpdateRequest req = new UpdateRequest();
        req.setAction(org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION.COMMIT, false, false);
        req.add(solrInputDocument);
        UpdateResponse solrResponse = req.process(solrServer);
        logger.trace("solr.add for {}: {}", cocktail, solrResponse);

        // MONGODB
        DBObject root = CocktailRepository.COCKTAIL_TO_BSON.apply(cocktail);
        WriteResult mongoResult = cocktails.save(root, WriteConcern.SAFE);
        logger.trace("mongo.save for {}: {}", cocktail, mongoResult);
    } catch (Exception e) {
        throw new RuntimeException("Exception updating " + cocktail, e);
    }
}

From source file:HAL.libraries.blackboard_client.data_classes.MongoDBConnection.java

License:Open Source License

/**
 * Creates a new Mongo client for the specified address.
 * @param address The ServerAddress where the host resides.
 * @throws GeneralMongoException Connecting to the database server failed.
 **///  w w  w.  j a va2  s  .  co  m
private MongoDBConnection(ServerAddress address) throws GeneralMongoException {
    try {
        //Logger.log(LogLevel.NOTIFICATION, "Starting a new mongoclient.");

        MongoOptions mongoOptions = new MongoOptions();
        mongoOptions.connectionsPerHost = Configuration.getPropertyInt(ConfigurationFiles.MONGO_DB_PROPERTIES,
                "connectionsPerHost");
        mongoOptions.threadsAllowedToBlockForConnectionMultiplier = Configuration.getPropertyInt(
                ConfigurationFiles.MONGO_DB_PROPERTIES, "threadsAllowedToBlockForConnectionMultiplier");

        mongoClient = new Mongo(address, mongoOptions);
        mongoClient.setWriteConcern(WriteConcern.SAFE);
    } catch (MongoException mongoException) {
        throw new GeneralMongoException("A mongo exception occurred while connecting.", mongoException);
    }
    this.address = address;
}

From source file:io.github.carlomicieli.springbooks.config.DataConfig.java

License:Apache License

@Bean
public MongoTemplate mongoTemplate() throws Exception {
    MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
    mongoTemplate.setWriteConcern(WriteConcern.SAFE);
    return mongoTemplate;
}

From source file:io.gravitee.repository.mongodb.common.MongoFactory.java

License:Apache License

private MongoClientOptions.Builder builder() {
    MongoClientOptions.Builder builder = MongoClientOptions.builder();

    builder.writeConcern(WriteConcern.SAFE);

    Integer connectionsPerHost = readPropertyValue(propertyPrefix + "connectionsPerHost", Integer.class);
    Integer connectTimeout = readPropertyValue(propertyPrefix + "connectTimeout", Integer.class, 500);
    Integer maxWaitTime = readPropertyValue(propertyPrefix + "maxWaitTime", Integer.class);
    Integer socketTimeout = readPropertyValue(propertyPrefix + "socketTimeout", Integer.class, 500);
    Boolean socketKeepAlive = readPropertyValue(propertyPrefix + "socketKeepAlive", Boolean.class);
    Integer maxConnectionLifeTime = readPropertyValue(propertyPrefix + "maxConnectionLifeTime", Integer.class);
    Integer maxConnectionIdleTime = readPropertyValue(propertyPrefix + "maxConnectionIdleTime", Integer.class);

    // We do not want to wait for a server
    Integer serverSelectionTimeout = readPropertyValue(propertyPrefix + "serverSelectionTimeout", Integer.class,
            0);//w  ww  .  j a v a  2  s.com
    Integer minHeartbeatFrequency = readPropertyValue(propertyPrefix + "minHeartbeatFrequency", Integer.class);
    String description = readPropertyValue(propertyPrefix + "description", String.class, "gravitee.io");
    Integer heartbeatConnectTimeout = readPropertyValue(propertyPrefix + "heartbeatConnectTimeout",
            Integer.class, 1000);
    Integer heartbeatFrequency = readPropertyValue(propertyPrefix + "heartbeatFrequency", Integer.class);
    Integer heartbeatSocketTimeout = readPropertyValue(propertyPrefix + "heartbeatSocketTimeout",
            Integer.class);
    Integer localThreshold = readPropertyValue(propertyPrefix + "localThreshold", Integer.class);
    Integer minConnectionsPerHost = readPropertyValue(propertyPrefix + "minConnectionsPerHost", Integer.class);
    Boolean sslEnabled = readPropertyValue(propertyPrefix + "sslEnabled", Boolean.class);
    Integer threadsAllowedToBlockForConnectionMultiplier = readPropertyValue(
            propertyPrefix + "threadsAllowedToBlockForConnectionMultiplier", Integer.class);
    Boolean cursorFinalizerEnabled = readPropertyValue(propertyPrefix + "cursorFinalizerEnabled",
            Boolean.class);

    if (connectionsPerHost != null)
        builder.connectionsPerHost(connectionsPerHost);
    if (maxWaitTime != null)
        builder.maxWaitTime(maxWaitTime);
    if (connectTimeout != null)
        builder.connectTimeout(connectTimeout);
    if (socketTimeout != null)
        builder.socketTimeout(socketTimeout);
    if (socketKeepAlive != null)
        builder.socketKeepAlive(socketKeepAlive);
    if (maxConnectionLifeTime != null)
        builder.maxConnectionLifeTime(maxConnectionLifeTime);
    if (maxConnectionIdleTime != null)
        builder.maxConnectionIdleTime(maxConnectionIdleTime);
    if (minHeartbeatFrequency != null)
        builder.minHeartbeatFrequency(minHeartbeatFrequency);
    if (description != null)
        builder.description(description);
    if (heartbeatConnectTimeout != null)
        builder.heartbeatConnectTimeout(heartbeatConnectTimeout);
    if (heartbeatFrequency != null)
        builder.heartbeatFrequency(heartbeatFrequency);
    if (heartbeatSocketTimeout != null)
        builder.heartbeatSocketTimeout(heartbeatSocketTimeout);
    if (localThreshold != null)
        builder.localThreshold(localThreshold);
    if (minConnectionsPerHost != null)
        builder.minConnectionsPerHost(minConnectionsPerHost);
    if (sslEnabled != null)
        builder.sslEnabled(sslEnabled);
    if (threadsAllowedToBlockForConnectionMultiplier != null)
        builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
    if (cursorFinalizerEnabled != null)
        builder.cursorFinalizerEnabled(cursorFinalizerEnabled);
    if (serverSelectionTimeout != null)
        builder.serverSelectionTimeout(serverSelectionTimeout);

    return builder;
}

From source file:me.philnate.textmanager.entities.Bill.java

License:Open Source License

@Override
public void delete() {
    LOG.debug(format("Deleting Bill %s, items", this));
    for (BillingItem item : BillingItem.find(getCustomer(), year, month)) {
        item.delete();//from  w  w  w. jav  a  2s  .  co  m
    }
    LOG.debug("Deleted all BillingItems, deleting Bill itself");
    ds.delete(this, WriteConcern.SAFE);
}

From source file:me.philnate.textmanager.entities.BillingItem.java

License:Open Source License

@Override
public void delete() {
    LOG.debug(format("Deleting BillingItem documents for %s", this));
    for (Document doc : documents) {
        doc.delete();/*ww  w. j av a 2s. com*/
    }
    LOG.debug("Deleting BillingItem itself");
    ds.delete(this, WriteConcern.SAFE);
}

From source file:me.philnate.textmanager.entities.Customer.java

License:Open Source License

@Override
public void delete() {
    LOG.debug(format("Deleting customer Bills for %s", this));
    for (Bill bill : Bill.find().filter("customerId", id).asList()) {
        bill.delete();/*w w w . j av a2  s  .co  m*/
    }
    LOG.debug("Deleting customer itself");
    ds.delete(this, WriteConcern.SAFE);
}