如何使用Windows Azure表存储时间戳或Etag字段

如何使用Windows Azure表存储时间戳或Etag字段,windows,node.js,azure,etag,Windows,Node.js,Azure,Etag,我正在Windows Azure网站(IISNode)上开发一个node.js应用程序,并为node.js安装了Azure SDK模块。我的问题是如何在表存储中使用etag或timestamp字段 是“我”做某事的问题,例如: if (entities[0].Timestamp == newEntity.Timestamp) // commit this update because we are dealing with the latest copy of this entity e

我正在Windows Azure网站(IISNode)上开发一个node.js应用程序,并为node.js安装了Azure SDK模块。我的问题是如何在表存储中使用etag或timestamp字段

是“我”做某事的问题,例如:

if (entities[0].Timestamp == newEntity.Timestamp)
    // commit this update because we are dealing with the latest copy of this entity
else
    // oops! one of the entities is newer than the other, better not save it
或者我需要监听tableService.updateEntity返回的错误(…返回的错误还是什么


任何帮助或建议

ETag用于更新期间的乐观并发。ETag在检索实体时自动设置,并在更新查询的实体时自动上载。如果实体同时被另一个线程更新,则更新将失败。updateEntity方法有一个选项nal参数checkEtag,可用于修改此行为,方法是允许您指定无论其他线程是否更新了记录,更新都应成功,从而覆盖乐观并发


Windows Azure存储服务REST API是Windows Azure存储的最终接口,当您想了解该服务的API功能时,htis是要参考的文档。通常,描述ETag的文档是。

正如Neil提到的,好的做法是让表服务处理与并发相关的服务凝灰岩

当我们使用客户端库从表服务获取实体时,库将把与实体相关联的ETag(存在于实体服务的响应中)存储在EntityDescriptor实例中(EntityDescriptor是库为上下文中的每个实体实例维护的对象)

当您提交更新实体的请求时,sdk将准备HTTP请求,其中body作为实体序列化为XML,ETag头作为请求的ETag值存储在实体对应的实体描述符中

当表服务接收到更新实体实例的请求时,它会检查请求中的ETag是否与表存储中当前与实体关联的ETag匹配(如果在接收更新请求之前发生了其他更新,则与服务中存储的实体关联的ETag会发生变化),如果不匹配,则服务将在响应中设置Http状态代码为412或409,从而返回前置条件失败/冲突错误

bool done = true;
while (!done)
{
    Stat statistics = _context.Execute<Stat>(new Uri("https://management.core.windows.net/<subscription-id>/services/storageservices/StatEntity?RowKey=..&PartitionKey=..").FirstOrDefault();

    try
    {
        // TODO: Update the entity, e.g. statistics.ReadCount++
        _context.UpdateObject(statistics);
        _context.SaveChanges();
        // success
        break;
    }
    catch (DataServiceRequestException exception)
    {
        var response = exception.Response.FirstOrDefault();
        if (response != null && (response.StatusCode == 412 || response.StatusCode == 409))
        {
            // Concurrency Exception - Entity Updated in-between
            // by another thread, Fetch the latest _stat entity and repeat operation
        }
        else
        {
            // log it and come out of while loop
            break;
        }
    }
    catch (Exception)
    {
        // log it and come out of while loop
        break;
    }
}
bool done=true;
而(!完成)
{
statistics=_context.Execute(新Uri(“https://management.core.windows.net//services/storageservices/StatEntity?RowKey=..&PartitionKey=..“”。FirstOrDefault();
尝试
{
//TODO:更新实体,例如statistics.ReadCount++
_context.UpdateObject(统计);
_SaveChanges();
//成功
打破
}
捕获(DataServiceRequestException异常)
{
var response=exception.response.FirstOrDefault();
if(response!=null&(response.StatusCode==412 | | response.StatusCode==409))
{
//并发异常-在这两个异常之间更新了实体
//通过另一个线程,获取最新的_stat实体并重复操作
}
其他的
{
//记录并退出while循环
打破
}
}
捕获(例外)
{
//记录并退出while循环
打破
}
}

感谢响应人员,那么当我遇到412或409错误时,如何处理?重新蚀刻记录并使用新数据再次更新记录?是否明智地构建重试循环,不断获取记录并尝试提交更改,直到成功?是的,您是对的,您可以使用重试循环。只需更新答案即可第例