Two parallel TransactionScope, each with its own connection

I'm trying to figure out how TransactionScope works with sample code representing various transaction scenarios ...
Can you explain what actually happens to the date when I have two parallel TransactionScopes, each of which has its own connection? Like in this code example:

internal class Program
{
    private const int Count = 20;
    private static readonly Random Random = new Random();

    private static void Main(string[] args)
    {
        var task1 = new Task(Task1);
        var task2 = new Task(Task2);
        task1.Start();
        task2.Start();
        Console.ReadLine();
    }
    private static void Task1()
    {
        var connection = new EntityConnection("name=ModelContainer");
        using (var transaction = new TransactionScope())
        {
            DoWork(connection);
            transaction.Complete();
        }
    }
    private static void Task2()
    {
        var connection = new EntityConnection("name=ModelContainer");
        using (var transaction = new TransactionScope())
        {
            DoWork(connection);
            transaction.Complete();
        }
    }
    private static void DoWork(EntityConnection connection)
    {
        connection.Open();
        using (var context = new ModelContainer(connection))
        {
            List<SyncData> list = context.SyncDataSet.ToList();
            for (int i = 0; i < Count; i++)
            {
                list[i].Knowledge.Version = Random.Next(200);
                context.SaveChanges();
            }
        }
    }
}
+3
source share
1 answer

, ( ), ( ) - DTC. , . , LTM, DTC , .

, , .. , . , .

+4

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


All Articles