Getting Started
Architecture
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Transactional Session with RavenDB Persistence

NuGet Package: NServiceBus.RavenDB.TransactionalSession (7.x)
Target Version: NServiceBus 7.x

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

Configuration

To enable the transactional session feature:

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

Opening a session

To open a RavenDB transactional session:

using var childBuilder = builder.CreateChildBuilder();
var session = childBuilder.Build<ITransactionalSession>();
await session.Open(new RavenDbOpenSessionOptions());

// use the session

await session.Commit();

Multi-tenancy support

The specific tenant database name is retrieved from message headers as configured in the SetMessageToDatabaseMappingConvention-method. This header needs to be set in the options so that the necessary information is available when storing operations and interacting with the outbox.

using var childBuilder = builder.CreateChildBuilder();
var session = childBuilder.Build<ITransactionalSession>();
await session.Open(
    new RavenDbOpenSessionOptions(
        new Dictionary<string, string>
        {
                // information is added to the message headers for the
                // `SetMessageToDatabaseMappingConvention`-method
                {"tenantDatabaseName", "tenantA-databaseName"}
        }));

// use the session

await session.Commit();

Transaction usage

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

await session.Open(new RavenDbOpenSessionOptions());

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

// access the database:
var ravenSession = session.SynchronizedStorageSession.RavenSession();

await session.Commit();

See the RavenDB shared session documentation for further details about using the transaction.

Related Articles