Cluster Transaction - Overview

Open Cluster-Wide Session

To open cluster transaction session you have to set the TransactionMode to TransactionMode.ClusterWide.

using (var session = store.OpenSession(new SessionOptions
{
    TransactionMode = TransactionMode.ClusterWide
}))
using (var session = store.OpenAsyncSession(new SessionOptions
{
    TransactionMode = TransactionMode.ClusterWide
}))

You can store, delete and edit document and the session will track them as usual.

Working with Compare Exchange

Get Compare Exchange

session.Advanced.ClusterTransaction.GetCompareExchangeValue<T>(key);
await session.Advanced.ClusterTransaction.GetCompareExchangeValueAsync<T>(key);
Parameters
key string The key to retrieve
Return Value
CompareExchangeValue<T> If the key doesn't exists it will return null

session.Advanced.ClusterTransaction.GetCompareExchangeValues<T>(keys);
await session.Advanced.ClusterTransaction.GetCompareExchangeValuesAsync<T>(keys);
Parameters
keys string[] Array of keys to retrieve
Return Value
Dictionary<string, CompareExchangeValue<T>> If a key doesn't exists the associate value will be null

Create Compare Exchange

session.Advanced.ClusterTransaction.CreateCompareExchangeValue(key, value);
session.Advanced.ClusterTransaction.CreateCompareExchangeValue(key, value);
Parameters
key string The key to save with the associate value. This string can be up to 512 bytes.
value T The value to store

If the value is already exists SaveChanges() will throw a ConcurrencyException.

// occupy "Best NoSQL Transactional Database" to be "RavenDB"
session.Advanced.ClusterTransaction.CreateCompareExchangeValue(key: "Best NoSQL Transactional Database", value: "RavenDB");
session.SaveChanges();
// occupy "Best NoSQL Transactional Database" to be "RavenDB"
session.Advanced.ClusterTransaction.CreateCompareExchangeValue(key: "Best NoSQL Transactional Database", value: "RavenDB");
await session.SaveChangesAsync();

Update Compare Exchange

session.Advanced.ClusterTransaction.UpdateCompareExchangeValue(new CompareExchangeValue<T>(key, index, value));
session.Advanced.ClusterTransaction.UpdateCompareExchangeValue(new CompareExchangeValue<T>(key, index, value));
Parameters
item CompareExchangeValue<T> The item to update

If the value was changed by someone else the SaveChanges() will throw a ConcurrencyException.

_async
                    // load the existing dns record of ravendb.net
                    CompareExchangeValue<DNS> result = await session.Advanced.ClusterTransaction.GetCompareExchangeValueAsync<DNS>(key: "ravendb.net");

                    // change the ip
                    result.Value.IpAddress = "52.32.173.150";
                    session.Advanced.ClusterTransaction.UpdateCompareExchangeValue(result);
                    
                    // save the changes
                    await session.SaveChangesAsync();
// load the existing dns record of ravendb.net
CompareExchangeValue<DNS> result = await session.Advanced.ClusterTransaction.GetCompareExchangeValueAsync<DNS>(key: "ravendb.net");

// change the ip
result.Value.IpAddress = "52.32.173.150";
session.Advanced.ClusterTransaction.UpdateCompareExchangeValue(result);

// save the changes
await session.SaveChangesAsync();

Delete Compare Exchange

session.Advanced.ClusterTransaction.DeleteCompareExchangeValue(key, index);
session.Advanced.ClusterTransaction.DeleteCompareExchangeValue(key, index);
Parameters
key string The key to save with the associate value
index long Index for concurrency control

CompareExchangeValue

Parameters
key string Key of the item to store. This string can be up to 512 bytes.
index long Index for concurrency control
value T The actual value to keep