Azure Table 存储优化并发处理?
Azure Table Storage optmistic concurrency handling?
在传统的关系数据库上,为了防止 Last Writer wins 的情况,更新通常是这样的:
update MyTable
set myColumn = @newValue,
version=version+1
where myPk = @pk and version = @versionObtainedPreviously
如何使用 Azure Table 存储实现类似的行为?
Azure 中的乐观并发 Table 存储通过实体上的 ETag
属性 处理。每当实体更新时,其 ETag 值都会更改。
使用乐观并发更新实体的过程如下所示:
- 您从 table 中获取实体。
- 您在客户端对实体进行更改(比如增加
version
属性)。
- 您将更新请求发送到 Table 存储。发送更新请求时,您需要包含所获取实体的 ETag 值。
当更新请求中包含 ETag 值时,Table 存储将该值与实体的当前 ETag 值进行比较。
如果两者相同,则表示实体在获取后尚未更新,可以进行更新。
如果值不同,则 Table 存储 returns 返回 Pre Condition failed (412)
错误。在这种情况下,您将需要再次获取实体并重复该过程。
从这个link
:
An entity's ETag provides default optimistic concurrency for update
operations. The ETag value is opaque and should not be read or relied
upon. Before an update operation occurs, the Table service verifies
that the entity's current ETag value is identical to the ETag value
included with the update request in the If-Match header. If the values
are identical, the Table service determines that the entity has not
been modified since it was retrieved, and the update operation
proceeds.
If the entity's ETag differs from that specified with the update
request, the update operation fails with status code 412 (Precondition
Failed). This error indicates that the entity has been changed on the
server since it was retrieved. To resolve this error, retrieve the
entity again and reissue the request.
To force an unconditional update operation, set the value of the
If-Match header to the wildcard character (*) on the request. Passing
this value to the operation will override the default optimistic
concurrency and ignore any mismatch in ETag values.
在传统的关系数据库上,为了防止 Last Writer wins 的情况,更新通常是这样的:
update MyTable
set myColumn = @newValue,
version=version+1
where myPk = @pk and version = @versionObtainedPreviously
如何使用 Azure Table 存储实现类似的行为?
Azure 中的乐观并发 Table 存储通过实体上的 ETag
属性 处理。每当实体更新时,其 ETag 值都会更改。
使用乐观并发更新实体的过程如下所示:
- 您从 table 中获取实体。
- 您在客户端对实体进行更改(比如增加
version
属性)。 - 您将更新请求发送到 Table 存储。发送更新请求时,您需要包含所获取实体的 ETag 值。
当更新请求中包含 ETag 值时,Table 存储将该值与实体的当前 ETag 值进行比较。
如果两者相同,则表示实体在获取后尚未更新,可以进行更新。
如果值不同,则 Table 存储 returns 返回 Pre Condition failed (412)
错误。在这种情况下,您将需要再次获取实体并重复该过程。
从这个link
:
An entity's ETag provides default optimistic concurrency for update operations. The ETag value is opaque and should not be read or relied upon. Before an update operation occurs, the Table service verifies that the entity's current ETag value is identical to the ETag value included with the update request in the If-Match header. If the values are identical, the Table service determines that the entity has not been modified since it was retrieved, and the update operation proceeds.
If the entity's ETag differs from that specified with the update request, the update operation fails with status code 412 (Precondition Failed). This error indicates that the entity has been changed on the server since it was retrieved. To resolve this error, retrieve the entity again and reissue the request.
To force an unconditional update operation, set the value of the If-Match header to the wildcard character (*) on the request. Passing this value to the operation will override the default optimistic concurrency and ignore any mismatch in ETag values.