在执行插入时,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 } }
这些链接应该给你一些指示