DocumentDb write within a transactionscope

╄→尐↘猪︶ㄣ 提交于 2020-01-04 03:32:05

问题


I am trying to use a DocumentDb write as a part of a transaction like below -

using (var scope = new TransactionScope)
{
//first transaction

//write to document db

//third transaction
}

I observed that if the third transaction fails, documentDb write is not rolled back and I still see the document in the collection. The first transaction (NEventStore in this case) rolls back perfectly. Does anyone know if DocumentDb supports TrnasactionScope. What if I have a nested transaction?

Thanks!

Edit: So looks like TransactionScope is not supported with DocumentDb and it knows nothing about them. Is there a way to make DocumentDb transactions part of an external transaction from C#? Has anyone come across this use case before?

Edit 2: Follow-up question and answer here as suggested


回答1:


DocumentDB operations are independent from TransactionScope. Once an operation returns, it's done. The database service doesn't know anything about TransactionScope and isn't connected to it in any way.

DocumentDB does have a transaction scope of its own, when working with server-side stored procedures. You can have multiple database calls within the stored proc, and if everything is successful, there's an implicit commit upon the stored procedure exiting. If something goes wrong and an exception is thrown, an implicit rollback is executed for all operations performed to the database within the stored procedure's scope.




回答2:


Lots of SQL users don't understand what to do where transactions are not available.

You should implement compensation logic your own or use frameworks like Windows Workflow Foundation. Compensation logic is related to Enterprise Integration Patterns. You also may use correlation ID pattern to check if big operation was done.

SQL people manage big operations in the same way when it is necessary to make long running transaction. https://www.amazon.com/Enterprise-Integration-Patterns-Designing-Deploying/dp/0321200683/ref=sr_1_1?ie=UTF8&qid=1480917322&sr=8-1&keywords=integration+patterns



来源:https://stackoverflow.com/questions/31819243/documentdb-write-within-a-transactionscope

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