-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSagaPersister_Update.cs
More file actions
37 lines (34 loc) · 1.7 KB
/
SagaPersister_Update.cs
File metadata and controls
37 lines (34 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Extensibility;
using NServiceBus.Persistence;
partial class SagaPersister
{
public Task Update(IContainSagaData sagaData, ISynchronizedStorageSession session, ContextBag context, CancellationToken cancellationToken = default)
{
return Update(sagaData, session, GetConcurrency(context), cancellationToken);
}
internal async Task Update(IContainSagaData sagaData, ISynchronizedStorageSession session, int concurrency, CancellationToken cancellationToken = default)
{
var sqlSession = session.SqlPersistenceSession();
var sagaInfo = sagaInfoCache.GetInfo(sagaData.GetType());
using (var command = sagaInfo.CreateCommand(sqlSession.Connection))
{
command.CommandText = sagaInfo.UpdateCommand;
command.Transaction = sqlSession.Transaction;
command.AddParameter("Id", sagaData.Id);
command.AddParameter("PersistenceVersion", StaticVersions.PersistenceVersion);
command.AddParameter("SagaTypeVersion", sagaInfo.CurrentVersion);
command.AddJsonParameter("Data", sqlDialect.BuildSagaData(command, sagaInfo, sagaData));
command.AddParameter("Concurrency", concurrency);
AddTransitionalParameter(sagaData, sagaInfo, command);
var affected = await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
if (affected != 1)
{
throw new Exception($"Optimistic concurrency violation when trying to save saga {sagaInfo.SagaType.FullName} {sagaData.Id}. Expected version {concurrency}.");
}
}
}
}