问题
Is there a way to make DocumentDb transactions part of an external transaction from C#?
This is a follow-up question from here and failure in transaction 3 below should rollback any documentDb updates.
using (var scope = new TransactionScope)
{
//first transaction
//write to document db
//third transaction
}
回答1:
I solved this by implementing IEnlistmentNotification on DocumentDbRepository interacting with DocumentDb n the following way -
- Implement the interface
- Make the current instance part of the Transaction.Current in the constructor. (Or before any update if you are using dependency injection and object are created beforehand. Using Lazy<> can also be considered for constructor invocation during the transaction.
- During create/update - save the old document, or the fact that the new object was created and save the selfLink.
- If Rollback is called, take corrective action with the information saved in the step above.
PS - I can share the pseudo code if anyone is interested, please leave a comment.
来源:https://stackoverflow.com/questions/31826887/documentdb-transactions-as-a-part-of-an-external-transaction-scope