Skip to main content

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
}))

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);
ParametersTypeDescription
keystringThe key to retrieve
Return ValueDescription
CompareExchangeValue<T>If the key doesn't exist it will return null

Get Multiple Values

session.Advanced.ClusterTransaction.GetCompareExchangeValues<T>(keys);
ParametersTypeDescription
keysstring[]Array of keys to retrieve
Return ValueDescription
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);
ParametersTypeDescription
keystringThe key to retrieve
keysstring[]Array of keys to retrieve
Return ValueDescription
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);
ParametersTypeDescription
keystringThe key to save with the associate value. This string can be up to 512 bytes.
valueTThe 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();

Delete Compare Exchange

session.Advanced.ClusterTransaction.DeleteCompareExchangeValue(key, index);
ParametersTypeDescription
keystringThe key to save with the associate value
indexlongIndex for concurrency control

CompareExchangeValue

ParametersTypeDescription
keystringKey of the item to store. This string can be up to 512 bytes.
indexlongIndex for concurrency control
valueTThe actual value to keep