SQL Azure 中的 TransactionScope()

发布于 2024-08-28 05:00:20 字数 831 浏览 12 评论 0原文

执行插入时,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)
                    {
                    }
                 }
             }

Does Sql Azure support using TransactionScope() when performing inserts? Below is a code snippet of what I am trying to do.

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)
                    {
                    }
                 }
             }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间海 2024-09-04 05:00:20

我的理解是,它适用于事务范围仅绑定到一个连接的情况。很多时候,因为最好的做法是晚打开连接并早关闭。可能存在范围跨越两个连接的情况。 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
       }
   }

这些链接也应该为您提供一些指导

My understanding is that it works in the case when the transaction scope is tied to only one connection. Often times because its a best practice to open a connection late and close early. there might be situation where the scope spans two connections. Those scenarios are not supported in sql azure.

An example of where it might not work is; taking yr example; Assuming MyDataContext.GetDataContext() returns a new instance of a connection.

   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
       }
   }

These links should give you some pointers aswell

笑忘罢 2024-09-04 05:00:20

Azure SQL 数据库分布式事务的快速更新:几天前,我们在 Azure SQL 数据库中引入了对分布式事务的支持。点亮该功能的特性称为弹性数据库事务。它重点关注使用 .NET 分布式事务 API(例如 TransactionScope)的场景。一旦安装了新的 4.6.1 版本的 .NET Framework,这些 API 就开始针对 Azure SQL 数据库工作。您可以在此处找到有关如何开始的更多信息:https://azure.microsoft.com/en-us/documentation/articles/sql-database-elastic-transactions-overview/

请尝试一下!

Quick update on distributed transactions with Azure SQL Database: A couple of days ago, we introduced support for distributed transactions in Azure SQL Database. The feature that lights up the capability is called elastic database transactions. It focuses on scenarios using the .NET distributed transaction APIs such as TransactionScope. These APIs start working against Azure SQL Database once you installed the new 4.6.1 release of the .NET framework. You can find more information about how to get started here: https://azure.microsoft.com/en-us/documentation/articles/sql-database-elastic-transactions-overview/.

Please give it a try!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文