Nested Transactions in LINQ to SQL

I need help implementing a rather complex business logic that runs on many tables and executes a lot of SQL commands. However, I want to be sure that the data will not remain in an unstable state, and at this point I do not see solutions that do not require nested transactions. I wrote a simple pseudo code that illustrates a script similar to what I want to execute:

Dictionary<int, bool> opSucceeded = new Dictionary<int, bool> ();

for (int i = 0; i < 10; i++)
{
    try
    {   
        // this operation must be atomic
        Operation(dbContext, i);

        // commit (?)

        opSucceeded[i] = true;
    }
    catch
    {
        // ignore
    }
}

try
{
    // this operation must know which Operation(i) has succeeded;
    // it also must be atomic
    FinalOperation(dbContext, opSucceeded);

    // commit all
}
catch
{
    // rollback FinalOperation and operation(i) where opSucceeded[i] == true
}

The biggest problem for me: how to ensure that when FinalOperation is completed, all Operation (i) operations that were completed successfully are rolled back? Note that I would also like to ignore the errors of one operation (i).

Is it possible to achieve this using TransactionScope nested objects, and if not, how would you approach this problem?

+3
1

, , , , ( ).

, , .

, , . "/", (, , FinalOperation ).

, , , FinalOperation. , .

, - ? ( , RETAIN / , ... , , ).

+2

Source: https://habr.com/ru/post/1698523/


All Articles