将不确定变为确定~transactionscope何时提升为分布式事务~续

回到目录

相关文章

将不确定变为确定~transactionscope何时提升为分布式事务

将不确定变为确定~transactionscope何时提升为分布式事务~续      

将不确定变为确定~transactionscope何时提升为分布式事务~再续(避免引起不必要的MSDTC)


 

          之前写过一篇关于《将不确定变为确定~transactionscope何时提升为分布式事务》的文章,但今天又测试了一下,发现前一篇文章有很多问题,这里再把问题说一下。

一 什么时间会把你的transactionscope提升为分布式事务,即要使用MSDTC服务

  1.   当你的WEB服务器与数据库服务器在同台电脑上,对同一个库进行操作时,它不会提升为分布式事务
  2.   当你的WEB服务器与数据库服务器在同台电脑上,对于同一个库,建立多个数据上下文时,它不会提升为分布式事务
  3.   当你的WEB服务器与数据库服务器在同台电脑上,当你操作两个库的表,这时才会提升为分布式事
  4.       当你的WEB服务器与数据库服务器不在同台电脑上,每次都会引发MSDTC

二 案例分析:

复制代码
 public class DbBase : Commons.Data.DbContextRepositoryBase
    {

        public DbBase()
            : base(Commons.Data.DbFactory.Intance(System.Configuration.ConfigurationManager.ConnectionStrings["testEntities1"].ToString(), 2, 0))
        {

        }
    }

    public class DbBase2 : Commons.Data.DbContextRepositoryBase
    {

        public DbBase2()
            : base(Commons.Data.DbFactory.Intance(System.Configuration.ConfigurationManager.ConnectionStrings["testEntities2"].ToString(), 2, 1))
        {

        }
    }
复制代码
复制代码
public class ReviewRepository : DbBase
    {

    }
    public class TestRepository : DbBase
    {
        public void Insert()
        {
            var product = new Product
            {
                Info = "test",
                Name = "test",
            };

            var product_Comment = new Product_Comment
            {
                CommentInfo = "test",
                CommentTitle = "Test",
                ProductID = 1,
                UserID = 1
            };
            var review = new Review
            {
                CreateDate = DateTime.Now,
                Info = "test",
                ObjID = 1,
                ObjType = 1,
                Status = 100,
                UserID = 1,
            };
            using (var trans = new TransactionScope())
            {
                //var testEntities = new testEntities();
                // var testEntities2 = new testEntities();
                #region  一个dbcontext对象不发生MSDTC
                //testEntities.Product.AddObject(product);
                //testEntities.Review.AddObject(review);
                //testEntities.SaveChanges();
                #endregion

                #region  多个dbcontext对象也不发生MSDTC
                //testEntities.Product.Add(product);
                //testEntities.SaveChanges();
                //testEntities2.Review.Add(review);
                //testEntities2.SaveChanges();
                #endregion

                #region 自己生产的DbContext对象也没有发生MSDTC
                //    base.Insert<Product>(product);
                base.Insert<Product_Comment>(product_Comment);
                new ReviewRepository().Insert<Review>(review);
                #endregion
                trans.Complete();
            }
        }
    }
复制代码

测试环境:SQLSERVER2008在一台服务器

     IIS7在别一台服务器

感谢阅读!

 

 回到目录

posted @   张占岭  阅读(1995)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2012-01-16 谁说LINQ复杂查询不支持返回实名类型~复杂结果集中再使用复杂结果集
点击右上角即可分享
微信分享提示