带锁的可靠字典中的并发事务

时间:2017-05-24 19:35:27

标签: .net azure-service-fabric

我正在使用ReliableCollection创建出价服务。我仍然对ReliableDictionary的锁定方面感到困惑。假设TX1出价为20美元,TX2出价为20美元,我有一个检查出价收集的逻辑,并且对于任何达到最后的交易都没有。写下面的代码,这是正确的做法还是可以改进以避免死锁?

using (var tx = this.AuctionStateManager.CreateTransaction())
        {
            //added an update lock for getting value
           var  bidOldState = await itemBidStore.TryGetValueAsync(tx,ItemId, LockMode.Update);

            if (bidOldState.HasValue)
            {
                //checking if its a valid bid or not. If No then abort the transaction
                var bidComputedRequest = BidCompute.ProcessBidState(bidOldState, bidNewRequest);

                //if it's a valid bid then add to dictionary
                await itemBidStore.AddOrUpdateAsync(tx: tx,
                                                   key: ItemId,
                                                   addValue: bidComputedRequest,
                                                   updateValueFactory: (key, value) => bidComputedRequest,
                                                   timeout: TimeSpan.FromMilliseconds(Constants.BidStoreAsyncTimeout),
                                                   cancellationToken: cancelRequest);

            }



            await tx.CommitAsync();
        }

0 个答案:

没有答案