NServicebus saga race condition
I am encountering a race condition when using NServiceBus sagas.
The problem is this-
I have a class that derives from Saga, implements IAmStartedByMessages and persists saga data in a sql server. This saga processes the incoming message, creates a new message, sends it on to another server. This server, processes the message and sends a response back. This reply message is then processed as the next stage of saga processing. I am using saga ids to configure the mapping.
Problem arises when the reply message immediately before the saga data has been saved to the database. Since the saga data has not been saved, the mapping fails and the message is lost.
To illustrate in the following example, say I have these handlers listening on different endpoints, I get AnotherMessage before the SagaData has been persisted. -
public class MySaga : Saga<SagaData>,
IAmStartedByMessages<StartMesssage>
HandleMessage<AnotherMessage>
{
public override void ConfigureHowToFindSaga()
{
ConfigureMapping<AnotherMessage>开发者_运维知识库;(s => s.Id, x => x.SagaId);
}
void Handle(StartMessage message)
{
var sendMsg = new SendMessage(){SagaId=this.Data.Id}
bus.Send(sendMsg)
}
void Handle(AnotherMessage message)
{
}
}
public class NextStage : IHandleMessages<SendMessage>
{
void Handle(SendMessage message)
{
var anotherMsg = new AnotherMessage() {SagaId=message.SagaId};
bus.Send(anotherMsg);
}
}
(Btw, I am using NServiceBus 2.x (the apache licence edition)
Regards,
Ilias
Since the persisting of the saga data and the bus.send will be part of the same transaction I don't see any posibilities for a race condition. Are you 100% sure that the data isn't persited before the reply arrives?
精彩评论