在aws-sdk(js)中使用transactWriteItems,我们得到了TransactionCanceledException。该异常内的原因为 TransactionConflict 。有时,事务中的所有操作都会失败,有时只有几个或只有一个失败。我们确实并行运行可以在同一项目上运行的多个事务。该文档没有提及此特定错误。可能原因摘录:
- A condition in one of the condition expressions is not met.
- A table in the TransactWriteItems request is in a different account or region.
- More than one action in the TransactWriteItems operation targets the same item.
- There is insufficient provisioned capacity for the transaction to be completed.
- 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.
- There is a user error, such as an invalid data format.
这些都不适用,并且在重试事务时似乎最终可以工作。有人知道这个异常(exception)吗?我找不到任何文件记录。
最佳答案
您遇到的不是bug,它实际上是功能的一部分,它在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并未使用锁定,而是使用了称为乐观并发控制的东西(也(令人困惑地)称为乐观锁定)。如果您想了解更多有关此的知识,那么Wikitia上有关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
关于amazon-web-services - TransactionCanceledException中的神秘TransactionConflict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54245599/