Azure 存储表 - 更新条件不满足
Azure Storage Tables - Update Condition Not Satisfied
当我尝试更新存储 table 上的实体时遇到随机异常。我得到的例外是
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();
}
}
我建议您可以考虑使用 Microsoft 企业库中的瞬态故障处理应用程序块,当您的应用程序在 Azure 中遇到此类瞬态故障时重试,以尽量减少每次重新启动服务器的时间出现同样的异常。
https://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50).aspx
在更新您的实体时,设置 ETag =“*”。
您修改后的代码应如下所示 -
if (entity != null)
{
entity.MyProperty= "newProperty";
tableContext.UpdateObject(entity);
tableContext.SaveChanges();
}
您发布的代码使用了非常旧的 table 层,现在已经过时了。我们强烈 建议您更新到更新版本的存储库并使用新的table 层。有关详细信息,请参阅此 Whosebug question。另请注意,如果您使用的是非常旧版本的存储库,它们最终将停止工作,因为它们使用的服务版本将在服务端被弃用。
我们不建议客户重复使用 TableServiceContext objects,就像这里所做的那样。它们包含各种可能导致性能问题以及其他不利影响的跟踪。这些限制是我们建议(如上所述)移动到较新的 table 层的部分原因。有关详细信息,请参阅 how-to。
在 table 实体 update operations 上,您必须发送 if-match header 指示 etag。如果您设置实体的 etag 值,库将为您设置。要更新服务上实体的 etag,请使用“*”。
当我尝试更新存储 table 上的实体时遇到随机异常。我得到的例外是
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();
}
}
我建议您可以考虑使用 Microsoft 企业库中的瞬态故障处理应用程序块,当您的应用程序在 Azure 中遇到此类瞬态故障时重试,以尽量减少每次重新启动服务器的时间出现同样的异常。
https://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50).aspx
在更新您的实体时,设置 ETag =“*”。 您修改后的代码应如下所示 -
if (entity != null)
{
entity.MyProperty= "newProperty";
tableContext.UpdateObject(entity);
tableContext.SaveChanges();
}
您发布的代码使用了非常旧的 table 层,现在已经过时了。我们强烈 建议您更新到更新版本的存储库并使用新的table 层。有关详细信息,请参阅此 Whosebug question。另请注意,如果您使用的是非常旧版本的存储库,它们最终将停止工作,因为它们使用的服务版本将在服务端被弃用。
我们不建议客户重复使用 TableServiceContext objects,就像这里所做的那样。它们包含各种可能导致性能问题以及其他不利影响的跟踪。这些限制是我们建议(如上所述)移动到较新的 table 层的部分原因。有关详细信息,请参阅 how-to。
在 table 实体 update operations 上,您必须发送 if-match header 指示 etag。如果您设置实体的 etag 值,库将为您设置。要更新服务上实体的 etag,请使用“*”。