Batches: How to send multiple commands using a batch?
To send multiple operations in a single request, reducing the number of remote calls and allowing several operations to share same transaction, Batch
should be used.
Syntax
BatchResult[] Batch(IEnumerable<ICommandData> commandDatas);
Parameters | ||
---|---|---|
An array of following commands: - PutCommandData - DeleteCommandData - PatchCommandData - ScriptedPatchCommandData |
ICommandData | Commands to process. |
Return Value | |
---|---|
BatchResult[] | An array of batch results matching exactly the order of commands send. |
Example
BatchResult[] results = store
.DatabaseCommands
.Batch(new ICommandData[]
{
new PutCommandData
{
Key = "products/999",
Document = RavenJObject.FromObject(new Product
{
Name = "My Product",
Supplier = "suppliers/999"
}),
Metadata = new RavenJObject()
},
new PutCommandData
{
Key = "suppliers/999",
Document = RavenJObject.FromObject(new Supplier
{
Name = "My Supplier",
}),
Metadata = new RavenJObject()
},
new DeleteCommandData
{
Key = "products/2"
}
});
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 ETag does not 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 attempted 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.