RavenDB version 2.5. Other versions:

Batch Operations

RavenDB supports batching multiple operations into a single request, reducing the number of remote calls and allowing several operations to share the same transactions.

Request batching in RavenDB is handled by the Client API using the Batch() method of DatabaseCommands, which accepts an array of operations to execute.

Batching PUT and DELETEs:

    var batchResults =
    	documentStore.DatabaseCommands.Batch(
    		new ICommandData[]
    			 {
    				 new PutCommandData
    					 {
    						 Document = RavenJObject.FromObject(
    						 new Company
    							 {
    								 Name = "Hibernating Rhinos"
    							 }), 
    							 Etag = null, 
    							 Key = "rhino1", 
    							 Metadata = new RavenJObject(),
    					 },
    				 new PutCommandData
    					 {
    						 Document = RavenJObject.FromObject(
    						 new Company
    							 {
    								 Name = "Hibernating Rhinos"
    							 }), 
    							 Etag = null, 
    							 Key = "rhino2", 
    							 Metadata = new RavenJObject(),
    					 },
    				 new DeleteCommandData
    					 {
    						 Etag = null, 
    						 Key = "rhino2"
    					 }
    			 });
    
    

Defer operation: In RavenDB when you call SaveChanges() all the changed document are batched into one call to the server and either they all pass or non will pass.
With the defer command we can add our own commands to this call:

    using (var session = documentStore.OpenAsyncSession())
    {
    	var commands = new ICommandData[]
    					{
    						new PutCommandData
    						{
    							Document =
    								RavenJObject.FromObject(new Company {Name = "Hibernating Rhinos"}),
    							Etag = null,
    							Key = "rhino1",
    							Metadata = new RavenJObject(),
    						},
    						new PutCommandData
    						{
    							Document =
    								RavenJObject.FromObject(new Company {Name = "Hibernating Rhinos"}),
    							Etag = null,
    							Key = "rhino2",
    							Metadata = new RavenJObject(),
    						}
    					};
    
    	session.Advanced.Defer(commands);
    	session.Advanced.Defer(new DeleteCommandData
    	{
    		Etag = null,
    		Key = "rhino2"
    	});
    }
    
    

Another operation supported by batching is the PATCH command.

Concurrency

If an etag is specified in the command, that etag is compared to the current etag on the document on the server. If the etags do no match, a 409 Conflict status code will be returned from the server, causing a ConcurrencyException to be thrown. In such a case, the entire operation fails and non of the updates that were tried will succeed.

Transactions

All the operations in the batch will succeed or fail as a transaction. Other users will not be able to see any of the changes until the entire batch completes.

Comments add new comment

The comments section is for user feedback or community content. If you seek assistance or have any questions, please post them at our support forums.

No comments found
SUBMIT COMMENT