0
我在Reliable Dictionary中遇到了一個問題,我更新了一個特定的條目,但不提交事務。看起來事務中止不會重置更新的條目。可靠的字典和交易提交
在我的邏輯中,我必須檢查幾個座位是否在可靠的字典中可用。如果是,我將它們分配給訂單。如果其中一個不可用,我理想中止交易,以確保先前分配的座位會回滾到原始狀態,因爲我沒有提交交易。
我可以在這裏做錯嗎?
這是我建立的代碼:
var unavailableSeats = new List<string>();
using (var tx = StateManager.CreateTransaction())
{
foreach (var requestSeat in request.Seats)
{
var match = await dict.Value.TryGetValueAsync(tx, requestSeat.ToString(), LockMode.Update);
if (!match.HasValue)
{
response.SetError($"No Seat found matching the Seat Key: {requestSeat} provided.");
return response;
}
var seatEntry = match.Value;
if (seatEntry.IsAvailable())
{
seatEntry.AssignToOrder(request.OrderId, request.RequestId.ToString());
await dict.Value.SetAsync(tx, requestSeat.ToString(), seatEntry, TimeSpan.FromSeconds(4),
cancellationToken);
}
else
{
unavailableSeats.Add(requestSeat.ToString());
}
}
if (!unavailableSeats.Any())
{
await tx.CommitAsync();
response.Success = true;
response.RequestId = request.RequestId;
return response;
}
tx.Abort();
}
您好託德,我實際上已經嘗試過了,並且回到了這種方法,因爲那也不管用。我要重新檢查。我在一個例子中下載的WebReferenceApp列出了這個方法,所以我嘗試了它。 –
啊!我看到了我的錯誤。我在更新內存中對象後克隆了條目。我應該先克隆然後修改它。改變代碼行來解決它。 –