Sql Azure中的TransactionScope()
发布时间:2020-12-12 16:19:58 所属栏目:MsSql教程 来源:网络整理
导读:在执行插入时,Sql Azure是否支持使用TransactionScope()?下面是我要做的事情的代码片段. using (var tx = new TransactionScope(TransactionScopeOption.RequiresNew,new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted })) { using
在执行插入时,Sql Azure是否支持使用TransactionScope()?下面是我要做的事情的代码片段.
using (var tx = new TransactionScope(TransactionScopeOption.RequiresNew,new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted })) { using (var db = MyDataContext.GetDataContext()) { try { MyObject myObject = new MyObject() { SomeString = "Monday" }; db.MyObjects.InsertOnSubmit(myObject); db.SubmitChanges(); tx.Complete(); } catch (Exception e) { } } } 解决方法我的理解是,它适用于事务范围仅与一个连接相关联的情况.通常是因为它是最佳实践,可以提前和关闭提前建立连接.可能存在范围跨越两个连接的情况. sql azure不支持这些场景.它可能不起作用的一个例子是;以你为榜样;假设MyDataContext.GetDataContext()返回连接的新实例. using (var tx = new TransactionScope(TransactionScopeOption.RequiresNew,new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted } )) { try{ DoSomething(); //a method with using (var db = MyDataContext.GetDataContext()) DoSomethingElse(); //another method with using (var db = MyDataContext.GetDataContext()) tx.Complete(); } catch { //Exception handler } } 这些链接应该给你一些指示 > Transactions in Sql Azure (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |