see on GitHub

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 documents 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 Type Description
key string The key to retrieve
Return Value Description
CompareExchangeValue<T> If the key doesn't exist it will return null

Get Multiple Values

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

Get Compare Exchange Lazily

// Single value
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValue<T>(key);

// Multiple values
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValues<T>(keys);
// Single value
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValueAsync<T>(key);

// Multiple values
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValuesAsync<T>(keys);
Parameters Type Description
key string The key to retrieve
keys string[] Array of keys to retrieve
Return Value Description
Lazy<CompareExchangeValue<T>> If the key doesn't exist it will return null
Lazy<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 Type Description
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();

Delete Compare Exchange

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

CompareExchangeValue

Parameters Type Description
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