RavenDB Persistence

Project Hosting | Nuget: NServiceBus.RavenDB (Version: 3.x)
Target NServiceBus Version: 5.x

Uses the RavenDB document database for storage.

Connection options for RavenDB

There are a variety of options for configuring the connection to a RavenDB Server. See RavenDB Connection Options for more details.

Shared session

NServiceBus supports sharing the same RavenDB document session between Saga persistence, Outbox persistence, and business data, so that a single transaction can be used to persist the data for all three concerns atomically.

Shared session is only applicable to Saga and Outbox storage. It can be configured via

Edit
var myDocumentStore = new DocumentStore();
// configure documentStore properties here

var persistence = busConfiguration.UsePersistence<RavenDBPersistence>();
persistence.UseSharedSession(() =>
{
    var session = myDocumentStore.OpenSession();
    // customize session here
    return session;
});

This optionally allows customization of the document session that is created for Saga, Outbox, and handler logic to share.

The session that is created is then made available.

Using in a Handler

Edit
public class HandlerThatUsesSession :
    IHandleMessages<MyMessage>
{
    ISessionProvider sessionProvider;

    public HandlerThatUsesSession(ISessionProvider sessionProvider)
    {
        this.sessionProvider = sessionProvider;
    }

    public void Handle(MyMessage message)
    {
        var doc = new MyDocument();

        sessionProvider.Session.Store(doc);
    }
}

Using in a Saga

Other than interacting with its own internal state, a saga should not access a database, call out to web services, or access other resources. See Accessing databases and other resources from a saga.

If the situation is special enough to warrant going against this recommendation, the following documentation will describe how to do so.

Edit
public class SagaThatUsesSession :
    Saga<SagaThatUsesSession.SagaData>,
    IHandleMessages<MyMessage>
{
    ISessionProvider sessionProvider;

    public SagaThatUsesSession(ISessionProvider sessionProvider)
    {
        this.sessionProvider = sessionProvider;
    }

    public void Handle(MyMessage message)
    {
        var document = new MyDocument();
        sessionProvider.Session.Store(document);
    }

Saga correlation

As of Version 6 of NServiceBus, all correlated properties are unique by default so there is no longer a configuration setting.

One of the limitations of the RavenDB persistence is support for only one [Unique] property (a saga property which value is guaranteed to be unique across all sagas of this type). Because of that limitation advanced user can turn off the validation that ensures sagas are only being found by unique properties:

Edit
var persistence = busConfiguration.UsePersistence<RavenDBPersistence>();
persistence.AllowStaleSagaReads();
This is a potentially dangerous feature that can result in multiple instances of saga being created instead of one in cases of high contention.

Distributed Transaction Coordinator settings

The RavenDB client requires a unique Guid to identify it to the Distributed Transaction Coordinator, and a method of storing DTC transaction recovery information in the case of process faults. By default, NServiceBus uses IsolatedStorageTransactionRecoveryStorage as its transaction recovery storage. Under certain high-load situations, this has been known to result in a TransactionAbortedException or IsolatedStorageException.

In order to set DTC settings that are safe for production use, refer to Setting RavenDB DTC settings manually.

Subscription persister and message versioning

The behavior of the RavenDB subscription persistence differs from other NServiceBus persisters in the way it handles versioning of message assemblies. It's important to understand this difference, especially when using a deployment solution that automatically increments assembly version numbers with each build.

To learn about message versioning as it relates to the RavenDB subscription persister, refer to RavenDB subscription versioning.

Viewing the data

Open a web browser and type the URL of the RavenDB server. This opens the RavenDB Studio.

Samples

Related Articles


Last modified