將不確定變為確定~transactionscope何時提升為分布式事務~續
相關文章
將不確定變為確定~transactionscope何時提升為分布式事務
將不確定變為確定~transactionscope何時提升為分布式事務~續
將不確定變為確定~transactionscope何時提升為分布式事務~再續(避免引起不必要的MSDTC)
之前寫過一篇關于《將不確定變為確定~transactionscope何時提升為分布式事務》的文章,但今天又測試了一下,發現前一篇文章有很多問題,這里再把問題說一下。
一 什么時間會把你的transactionscope提升為分布式事務,即要使用MSDTC服務
- 當你的WEB服務器與數據庫服務器在同臺電腦上,對同一個庫進行操作時,它不會提升為分布式事務
- 當你的WEB服務器與數據庫服務器在同臺電腦上,,對于同一個庫,建立多個數據上下文時,它不會提升為分布式事務
- 當你的WEB服務器與數據庫服務器在同臺電腦上,,當你操作兩個庫的表,這時才會提升為分布式事
- 當你的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在別一臺服務器
感謝閱讀!
浙公網安備 33010602011771號