Searching...
Tuesday, 10 December 2013

Spring Data Mongodb WriteConcern Example

08:15

Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for for new data stores while retaining store-specific features and capabilities. See more about Spring Data

WriteConcern

Write concern describes the guarantee that MongoDB provides when reporting on the success of a write operation. The strength of the write concerns determine the level of guarantee it control the write behavior for with various options, as well as exception raising on error conditions. The available write concerns are..

-1 =No exceptions are raised, even for network issues

0 = Exceptions are raised for network issues, but not server errors

1 = Exceptions are raised for network issues, and server errors; waits on a server for the write operation

2 = Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation*/

In this example we will see how to set WriteConcern while saving mongodb object and its output.

1. Create a Maven Project

Create a Maven project and add below dependencies

<repositories>
		<repository>
			<id>spring-milestone</id>
			<name>Spring Maven MILESTONE Repository</name>
			<url>http://maven.springframework.org/milestone</url>
		</repository>
	</repositories>
	<dependencies>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<!-- mongodb java driver -->
		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>2.5.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
			<version>1.0.0.M2</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2</version>
		</dependency>
	</dependencies>

2. Create a Main class to save objects

package com.pretech;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
public class SpringDataWriteConcern {
	public static void main(String[] args) {
		Mongo mongo;
		try {
			mongo = new Mongo("localhost");
			MongoTemplate mongoTemplate = new MongoTemplate(mongo, "PersonDB");
			mongoTemplate.setWriteConcern(WriteConcern.NONE);
			System.out.println("Person Details Save Transaction Started");
			mongoTemplate.save(new Person("Ramu"));
			System.out.println("Person Details Save Transaction Completed");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
class Person {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Person(String name) {
		super();
		this.name = name;
	}
}

Run it


In this example we are using WriteConcern.NONE ( –1) it means even for network issues also the program will continue. For testing shutdown mongodb and run the above program, we can see the completed message is printing.



Person Details Save Transaction Started
Dec 10, 2013 9:41:57 PM com.mongodb.DBTCPConnector fetchMaxBsonObjectSize
WARNING: null
java.io.IOException: couldn't connect to [vinod-PC/192.168.211.1:27017] bc:java.net.ConnectException: Connection refused: connect
    at com.mongodb.DBPort._open(DBPort.java:205)
    at com.mongodb.DBPort.go(DBPort.java:94)
    at com.mongodb.DBPort.go(DBPort.java:75)
    at com.mongodb.DBPort.findOne(DBPort.java:129)
    at com.mongodb.DBPort.runCommand(DBPort.java:138)
    at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:422)
    at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:526)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:236)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:209)
    at com.mongodb.DBCollection.insert(DBCollection.java:66)
    at com.mongodb.DBCollection.save(DBCollection.java:624)
    at org.springframework.data.mongodb.core.MongoTemplate$10.doInCollection(MongoTemplate.java:876)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:401)
    at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:867)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:799)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:745)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:736)
    at com.pretech.SpringDataWriteConcern.main(SpringDataWriteConcern.java:17)

Dec 10, 2013 9:41:58 PM com.mongodb.DBTCPConnector fetchMaxBsonObjectSize
WARNING: null
java.io.IOException: couldn't connect to [vinod-PC/192.168.211.1:27017] bc:java.net.ConnectException: Connection refused: connect
    at com.mongodb.DBPort._open(DBPort.java:205)
    at com.mongodb.DBPort.go(DBPort.java:94)
    at com.mongodb.DBPort.go(DBPort.java:75)
    at com.mongodb.DBPort.findOne(DBPort.java:129)
    at com.mongodb.DBPort.runCommand(DBPort.java:138)
    at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:422)
    at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:409)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:153)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:146)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:254)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:209)
    at com.mongodb.DBCollection.insert(DBCollection.java:66)
    at com.mongodb.DBCollection.save(DBCollection.java:624)
    at org.springframework.data.mongodb.core.MongoTemplate$10.doInCollection(MongoTemplate.java:876)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:401)
    at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:867)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:799)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:745)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:736)
    at com.pretech.SpringDataWriteConcern.main(SpringDataWriteConcern.java:17)

Dec 10, 2013 9:41:59 PM com.mongodb.DBTCPConnector$MyPort error
SEVERE: MyPort.error called
java.io.IOException: couldn't connect to [vinod-PC/192.168.211.1:27017] bc:java.net.ConnectException: Connection refused: connect
    at com.mongodb.DBPort._open(DBPort.java:205)
    at com.mongodb.DBPort.go(DBPort.java:94)
    at com.mongodb.DBPort.go(DBPort.java:75)
    at com.mongodb.DBPort.say(DBPort.java:70)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:160)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:146)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:254)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:209)
    at com.mongodb.DBCollection.insert(DBCollection.java:66)
    at com.mongodb.DBCollection.save(DBCollection.java:624)
    at org.springframework.data.mongodb.core.MongoTemplate$10.doInCollection(MongoTemplate.java:876)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:401)
    at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:867)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:799)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:745)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:736)
    at com.pretech.SpringDataWriteConcern.main(SpringDataWriteConcern.java:17)

Person Details Save Transaction Completed


0 comments: