netcore 事务及嵌套事务在多线程下的执行

1.上代码,使用TransactionScope 类库作事务测试

 //主代码    
 public static void Main(string[] args)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    Console.WriteLine("------------------------------------------------------------");
                    Console.WriteLine("我是-主线程-主事务");
                    Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
                    Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
                    new IDLogTester().IdTest(1);

                    using (TransactionScope ts2 = new TransactionScope())
                    {
                        Console.WriteLine("------------------------------------------------------------");
                        Console.WriteLine("我是-主线程-子事务-继承");
                        Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
                        Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
                        new IDLogTester().IdTest(2);

                        ts2.Complete();
                    }

                    Task.Run(() =>
                    {
                        using (TransactionScope ts3 = new TransactionScope())
                        {
                            Console.WriteLine("------------------------------------------------------------");
                            Console.WriteLine("我是-子线程-子事务-继承");
                            Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
                            Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
                            new IDLogTester().IdTest(3);
                           
                            ts3.Complete();
                        }
                    });

                  

                    using (TransactionScope ts4 = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        Console.WriteLine("------------------------------------------------------------");
                        Console.WriteLine("我是-主线程-子事务-新建");
                        Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
                        Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
                        new IDLogTester().IdTest(4);


                        ts4.Complete();
                    }

                    using (TransactionScope ts5 = new TransactionScope())
                    {
                        Console.WriteLine("------------------------------------------------------------");
                        Console.WriteLine("我是-主线程-子事务-继承-反射");
                        Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
                        Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
                        string nspace = "ClassLibrary3";
                        var ns = Assembly.Load(nspace);
                        var obj = ns.CreateInstance($"{nspace}.IDLogTester_Other") as IDLogTester_Other;
                        obj.IdTest(5);
                        ts5.Complete();
                    }

                    Console.WriteLine("------------------------------------------------------------");
                    Console.WriteLine("我是-主线程-主事务-反射-子事务-继承");
                    Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
                    Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
                    string nspace1 = "ClassLibrary3";
                    var ns1 = Assembly.Load(nspace1);
                    var obj1 = ns1.CreateInstance($"{nspace1}.IDLogTester_Other") as IDLogTester_Other;
                    obj1.IdTest_ts(6);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    ts.Complete();
                    Console.WriteLine(ex.Message);
                }
            }
//反射使用的代码
public class IDLogTester_Other: DbContext<IDLog>
    {
        public void IdTest(long id)
        {
            IDLog model = new IDLog();
            model.ID = id;
            model.Dt = DateTime.Now;
            Db.Insertable(model).ExecuteCommand();
        }
        public void IdTest_ts(long id)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                IDLog model = new IDLog();
                model.ID = id;
                model.Dt = DateTime.Now;
                Db.Insertable(model).ExecuteCommand();
                //throw new Exception("error");
                ts.Complete();
            }

        }
    }

2.结果和总结

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值