Getting Started
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Previews
Samples

Transactional Session with CosmosDB Persistence

NuGet Package: NServiceBus.Persistence.CosmosDB.TransactionalSession (1.x)
Target Version: NServiceBus 7.x

In order to use the transactional session feature with CosmosDB Persistence, add a reference to the NServiceBus.Persistence.CosmosDB.TransactionalSession NuGet package.

Configuration

To enable the transactional session feature:

var persistence = config.UsePersistence<CosmosPersistence>();
persistence.EnableTransactionalSession();

Opening a session

To open a CosmosDB transactional session, a partition key must be provided:

using var childBuilder = builder.CreateChildBuilder();
var session = childBuilder.Build<ITransactionalSession>();
await session.Open(
        new CosmosOpenSessionOptions(
            new PartitionKey("MyPartitionKey")))
    .ConfigureAwait(false);

// use the session

await session.Commit()
    .ConfigureAwait(false);

Custom container

By default, the transactional session uses the configured default container. The CosmosOpenSessionOptions instance can be configured with container information to be used for this transaction:

using var childBuilder = builder.CreateChildBuilder();
var session = childBuilder.Build<ITransactionalSession>();
await session.Open(
        new CosmosOpenSessionOptions(
            new PartitionKey("MyPartitionKey"),
            new ContainerInformation(
                "MyContainer",
                new PartitionKeyPath("/path/to/partition/key"))))
    .ConfigureAwait(false);

// use the session

await session.Commit().
    ConfigureAwait(false);

Transaction usage

Message and database operations made via the the transactional session are committed together once the session is committed:

await session.Open(
        new CosmosOpenSessionOptions(
            new PartitionKey("MyPartitionKey")))
    .ConfigureAwait(false);

// add messages to the transaction:
await session.Send(new MyMessage())
    .ConfigureAwait(false);

// access the database:
var cosmosSession = session.SynchronizedStorageSession.CosmosPersistenceSession();

await session.Commit()
    .ConfigureAwait(false);

See the Cosmos DB persistence transactions documentation for further details about using the transaction.

In order to guarantee atomic consistency across message and database operations, the outbox must be enabled. Otherwise Commit executes database modifications first and then messages are dispatched with best-effort.

Related Articles


Last modified