gpt4 book ai didi

Azure 表存储乐观并发处理?

转载 作者:行者123 更新时间:2023-12-03 05:23:48 25 4
gpt4 key购买 nike

在传统的关系数据库上,为了防止“最后写入者获胜”的情况,更新通常如下完成:

update MyTable
set myColumn = @newValue,
version=version+1
where myPk = @pk and version = @versionObtainedPreviously

如何使用 Azure 表存储实现类似的行为?

最佳答案

Azure 表存储中的乐观并发是通过实体上的 ETag 属性来处理的。每当实体更新时,其 ETag 值都会发生变化。

使用乐观并发更新实体的过程如下所示:

  1. 您从表中获取实体。
  2. 您在客户端对实体进行更改(例如增加 version 属性)。
  3. 您将更新请求发送到表存储。发送更新请求时,您需要包含所获取实体的 ETag 值。

当更新请求中包含 ETag 值时,表存储会将该值与实体的当前 ETag 值进行比较。

如果两者相同,则意味着该实体自获取以来尚未更新,可以进行更新。

如果值不同,则表存储将返回前提条件失败 (412) 错误。在这种情况下,您将需要再次获取实体并重复该过程。

从此link :

An entity's ETag provides default optimistic concurrency for updateoperations. The ETag value is opaque and should not be read or reliedupon. Before an update operation occurs, the Table service verifiesthat the entity's current ETag value is identical to the ETag valueincluded with the update request in the If-Match header. If the valuesare identical, the Table service determines that the entity has notbeen modified since it was retrieved, and the update operationproceeds.

If the entity's ETag differs from that specified with the updaterequest, the update operation fails with status code 412 (PreconditionFailed). This error indicates that the entity has been changed on theserver since it was retrieved. To resolve this error, retrieve theentity again and reissue the request.

To force an unconditional update operation, set the value of theIf-Match header to the wildcard character (*) on the request. Passingthis value to the operation will override the default optimisticconcurrency and ignore any mismatch in ETag values.

关于Azure 表存储乐观并发处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70311132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com