Sql Azure中的TransactionScope()

在执行插入时,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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...