两个并行的 TransactionScope,每个都有自己的连接

发布于 2024-10-07 03:01:52 字数 1426 浏览 4 评论 0原文

我试图弄清楚 TransactionScope 如何使用代表不同事务场景的示例代码来工作......
您能解释一下,当我有 2 个并发 TransactionScope,每个都有自己的连接时, date 实际发生了什么?就像这个示例代码一样:

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();
            }
        }
    }
}

I'm trying to figure out how TransactionScope works playing around with sample code representing different transactions scenarios...
Can you explain what actually happens with date when I have 2 concurrent TransactionScopes each with it's own connection? Like in this sample code:

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();
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2024-10-14 03:01:52

嵌套的事务范围(并且在同一线程上)成为同一事务的一部分(外部事务是完成的定义范围) - 如果需要,则升级到 DTC。在您的情况下,事务似乎不是嵌套的,因此它们是不相关的。这也意味着 LTM 而不是 DTC 是所有者,其开销较少。

因此,当然,如果它们接触相同的数据,您可能需要注意它们之间的阻塞等。特别是因为事务范围默认为可序列化隔离。

Transaction scopes that are nested (and on the same thread) become part of the same transaction (the outer transaction being the defining scope for completion) - escalating to DTC if needed. In your case the transactions don't seem nested, so they are unrelated. That should also mean that the LTM rather than DTC is the owner, which has less overhead.

As a result, of course, you may need to watch for blocking etc between these if they touch the same data. Especially since transaction scope defaults to serializable isolation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文