TransactionCanceledException 中的神秘 TransactionConflict

Mysterious TransactionConflict in TransactionCanceledException

使用transactWriteItems in the aws-sdk (js) we get a TransactionCanceledException。该异常中的原因给出为 TransactionConflict。有时交易中的所有动作都失败,有时只有少数或只有一个。我们并行执行 运行 多个可以对相同项目进行操作的事务。该文档未提及此特定错误。可能的原因摘录:

  1. A condition in one of the condition expressions is not met.
  2. A table in the TransactWriteItems request is in a different account or region.
  3. More than one action in the TransactWriteItems operation targets the same item.
  4. There is insufficient provisioned capacity for the transaction to be completed.
  5. An item size becomes too large (larger than 400 KB), or a local secondary index (LSI) becomes too large, or a similar validation error occurs because of changes made by the transaction.
  6. There is a user error, such as an invalid data format.

None 这些适用并且在重试事务时它似乎最终工作。有人知道这个例外吗?我找不到任何记录。

您遇到的不是错误——它实际上是功能的一部分,并且在 launch announcement 中提到过。

Items are not locked during a transaction. DynamoDB transactions provide serializable isolation. If an item is modified outside of a transaction while the transaction is in progress, the transaction is canceled and an exception is thrown with details about which item or items caused the exception.

顺便说一句,DynamoDB 没有使用锁定,而是使用一种称为乐观并发控制的东西(也(容易混淆地)称为乐观锁定)。如果您有兴趣了解更多相关信息,Optimistic Concurrency Control 上的维基百科文章非常好。

回到手头的问题,AWS documentation 交易说:

Multiple transactions updating the same items simultaneously can cause conflicts that cancel the transactions. We recommend following DynamoDB best practices for data modeling to minimize such conflicts.

专门针对 TransactWriteItems,他们说:

Write transactions don't succeed under the following circumstances:

When an ongoing TransactWriteItems operation conflicts with a concurrent TransactWriteItems request on one or more items in the TransactWriteItems operation. In this case, the concurrent request fails with a TransactionCancelledException

与 TransactGetItems 类似:

Read transactions don't succeed under the following circumstances:

When there is an ongoing TransactGetItems operation that conflicts with a concurrent PutItem, UpdateItem, DeleteItem or TransactWriteItems request. In this case the TransactGetItems operation fails with a TransactionCancelledException