Miguel Domingos
Miguel Domingos

Reputation: 353

NServiceBus Data saved but no saga saved

We’re using NServiceBus 4.6.5, with Oracle database, and for a specific request, intermittently the data is saved into our database, but the corresponding sagaData is not saved to the database.

I looked at the logs and can’t find an explanation.

If you need me to post here more information just let me know, as I have no idea where to start.

Thanks, Miguel

EDIT1: Our sagas are being saved into the database for most messages, it's just for a specific message that this is happening.

It is happening intermittently (only happens for some messages of that type).

And the normal CRUD operations for that message type are persisted, just the SagaData is not).

Maybe there's something wrong with the Distributed Transaction Coordinator, or maybe it is a NServiceBus 4.6.5 bug?

EDIT2: The code is a little offuscated:
public partial class MySaga : IHandleMessages<IAddMyCommand>
{
    public void Handle(IAddMyCommand cmd) {
        ...
        cmd.ExternalCombinedIdentifier = ...;
        Data.ExternalCombinedIdentifier = cmd.ExternalCombinedIdentifier;
        ...
        var myId = SagaUtilities.GetById(cmd.ExternalCombinedIdentifier, ...);
        ...
        var response = Repository.AddObject(...);
        ...
        Data.MyData.Add(new MyData { ... MyId = response.MyId, Timestamp = response.Timestamp, ... });
        ...
    }
    ...
[SagaIndex("ExternalCombinedIdentifier")]
public class MySagaData : IContainSagaData
{

    public long ExternalCombinedIdentifier { get; set; }

    ...
}

EDIT 3: We have a Master and 2 Worker Servers. Might it be the case that the SagaData in 1 server is out of sync with the SagaData in another server? We persist the SagaData to Oracle.

How does NServiceBus guarantee that the SagaData in our situation is in sync in the 3 servers (Master and Workers)?

Upvotes: 0

Views: 451

Answers (1)

Ross Sandford
Ross Sandford

Reputation: 21

There are a few elements that you need to have in place when persisting saga data. Your class that contains your saga implementation should inherit from the Saga base class. To do this, you'll need to specify a custom class (YourSagaDataClass in the example below) that will be stored using your persistence layer:

public class MySagaClass : Saga<YourSagaDataClass>

YourSagaDataClass must implement the IContainSagaData interface, as it will be used to persist saga data, and to correlate the handling of messages with their correct saga, usually using a CorrelationId. Indeed, implementing the IContainSagaData interface gives you these three properties:

public Guid Id { get; set; }
public string Originator { get; set; }
public string OriginalMessageId { get; set; }

To work with the persisted saga data in a message or event handler, use Data as a reference to your persisted saga data:

Data.OrderId = message.OrderId;

When persisting saga data with NServiceBus, also ensure that you have specified a saga persistence.

More information from Particular Software themselves can be found here: https://docs.particular.net/nservicebus/sagas/?version=core_4

Upvotes: 1

Related Questions