【问题标题】:Azure Storage Tables - Update Condition Not SatisfiedAzure 存储表 - 不满足更新条件
【发布时间】:2015-10-23 02:08:41
【问题描述】:

当我尝试更新存储表上的实体时出现随机异常。我得到的例外是

System.Data.Services.Client.DataServiceRequestException: An error occurred while processing this request. ---> System.Data.Services.Client.DataServiceClientException: {"odata.error":{"code":"UpdateConditionNotSatisfied","message":{"lang":"en-US","value":"The update condition specified in the request was not satisfied.\nRequestId:2a205f10-0002-013b-028d-0bbec8000000\nTime:2015-10-20T23:17:16.5436755Z"}}} ---

我知道这可能是一个并发问题,但问题是没有其他进程访问该实体。

我有时会遇到几十个这样的异常,我重新启动服务器,它又开始正常工作了。

public static class StorageHelper
    {
        static TableServiceContext tableContext;

        static CloudStorageAccount storageAccount;

        static CloudTableClient CloudClient;



        static StorageHelper()
        {

                storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
                CloudClient = storageAccount.CreateCloudTableClient();
                tableContext = CloudClient.GetTableServiceContext();
                tableContext.IgnoreResourceNotFoundException = true;
            }



public static void Save(int myId,string newProperty,string myPartitionKey,string myRowKey){
     var entity = (from j in tableContext.CreateQuery<MyEntity>("MyTable")
                          where j.PartitionKey == myId
                          select j).FirstOrDefault();


            if (entity != null)
            {
                entity.MyProperty= myProperty;
                tableContext.UpdateObject(entity);
                tableContext.SaveChanges();


            }
            else
            {
                entity = new MyEntity();
                entity.PartitionKey =MyPartitionKey;
                entity.RowKey =MyRowKey;
                entity.MyProperty= myProperty;
                tableContext.AddObject("MyTable", entity);
                tableContext.SaveChanges();
            }
}

【问题讨论】:

  • 什么是tableContext?这段代码 sn -p 实际上并没有显示对存储库的任何请求。
  • 好的,我用全班更新了代码。

标签: azure azure-blob-storage


【解决方案1】:
  1. 您发布的代码使用了现在已经过时的非常旧的表格层。我们强烈建议您更新到较新版本的存储库并使用新的表层。有关更多信息,请参阅此StackOverflow question。另请注意,如果您使用的是非常旧版本的存储库,它们最终将停止工作,因为它们使用的服务版本将被服务端弃用。

  2. 我们不建议客户重复使用 TableServiceContext 对象,就像这里所做的那样。它们包含各种可能导致性能问题以及其他不利影响的跟踪。这些限制是我们建议(如上所述)迁移到较新的表层的部分原因。请参阅how-to 了解更多信息。

  3. 在表实体update operations 上,您必须发送一个指示 etag 的 if-match 标头。如果您设置实体的 etag 值,库将为您设置此项。要更新服务上实体的任何 etag,请使用“*”。

【讨论】:

  • 谢谢艾米丽,我会检查你所说的一切并尝试尽快更新(它不是那么旧,我在 2 年前编写了该代码,这是这样做的方法:)跨度>
【解决方案2】:

我建议您可以考虑使用微软企业库中的瞬态故障处理应用程序块在您的应用程序在 Azure 中遇到此类瞬态故障时重试,以尽量减少每次发生相同异常时重新启动服务器。

https://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50).aspx

【讨论】:

    【解决方案3】:

    更新实体时,设置 ETag = "*"。 您修改后的代码应如下所示 -

    if (entity != null)
    {
        entity.MyProperty= "newProperty";
        tableContext.UpdateObject(entity);
        tableContext.SaveChanges();
    }
    

    【讨论】:

    • Mayur,我没有看到您共享的代码中有任何修改。你有什么遗漏吗?
    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2014-12-01
    • 2023-03-14
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多