How to do distributed transaction cordination around SQL API and GraphDB in CosmosDB?

会有一股神秘感。 提交于 2019-12-02 08:30:10

Since you don't have transactions in Cosmos DB across different collections (only within the partition of one container), this won't be directly possible.

Next best thing could be to use the Change Feed. It gets triggered whenever an item gets changed or inserted. But: It does not get triggered on deletes. So you need another little workaround of "soft deletes". Bascially you create a flag to that document ("to-be-deleted" etc.) and set its TTL to something very soon. This does trigger then change feed and you can from there delete the item in the other collection.

Is all that better than what you currently have? Honestly, not really if you ask me.

//Update: To add to the point regarding commit/rollback: This also does not exist in Cosmos DB. One possible workaround for this that comes to mind:

  1. Update elements in collection shopping cart. Set a flag to-be-deleted to true and set the TTL for those elements to something like now() + 5 minutes
  2. Delete the element in customer collection. If this works, all good.
  3. If deletion failed, update the shoppingcart again. Remove the to-be-deleted flag and remove the TTL so Comsos DB won't automatically delete it.

Of course, you also need to update any queries you run against your shoppingcart to exclude any elements with the deletion flag in place.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!