SQL Azure Transactions
来源:互联网 发布:玛格南图片社 知乎 编辑:程序博客网 时间:2024/06/05 07:25
Handling Transactions in SQL Azure
http://social.technet.microsoft.com/wiki/contents/articles/handling-transactions-in-sql-azure.aspx
Local Transactions
SQL Azure supports local transactions. These types of transactions are done with the Transact-SQL commands BEGIN TRANSACTION , ROLLBACK TRANSACTION , COMMIT TRANSACTION . They work exactly the same as they do on SQL Server.
Distributed Transactions in SQL Azure
SQL Azure Database does not support distributed transactions, which are transactions that multiple transaction managers (multiple resources). For more information, see Distributed Transactions (ADO.NET) . This means that SQL Azure doesn’t allow Microsoft Distributed Transaction Coordinator (MS DTC)to delegate distributed transaction handling. Because of this you can’t use ADO.NET or MSDTC to commit or rollback a single transaction that spans across multiple SQL Azure databases or a combination of SQL Azure and an on-premise SQL Server.
This doesn’t mean that SQL Azure doesn’t support transactions, it does. However,it only supports transactions that are not escalated to a resource manager such as MS DTC. An article entitled: Transaction Management Escalation on MSDN can give you more information.
TransactionScope and SqlTransaction
The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. The TransactionScope class works with the Transaction Managerto determine how the transaction will be handled. If the transaction manager determines that the transaction should be escalated to a distributed transaction, using theTransactionScope class will cause a runtime exception when running commands against SQL Azure, since distributed transactions are not supported.
One way to write your code without using the TransactionScope class is to use SqlTransaction. TheSqlTransaction class doesn’t use the transaction manager, it wraps the commands within a local transaction that is committed when you call the Commit() method. You still can’t have a single transaction across multiple databases; however SqlTransaction class provides a clean way in C# to wrap the commands. If your code throws an exception,the using statement guarantees a call to IDispose which rolls back the transaction.
using (SqlConnection sqlConnection = new SqlConnection(ConnectionString)){ sqlConnection.Open(); using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction()) { // Createthe SqlCommand object and execute the first command. SqlCommand sqlCommand = new SqlCommand("sp_DoFirstPieceOfWork", sqlConnection, sqlTransaction); sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; sqlCommand.ExecuteNonQuery(); // Createthe SqlCommand object and execute the first command. SqlCommand sqlCommand = new SqlCommand("sp_DoSecondPieceOfWork", sqlConnection, sqlTransaction); sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; sqlCommand.ExecuteNonQuery(); sqlTransaction.Commit(); }}
- SQL Azure Transactions
- Sql Sever Transactions详解
- Receiving Transactions SQL
- T-SQL Nested procedure & transactions
- Transactions
- SQL Transactions with the SqlTransaction Class (ZT)
- SQL Azure十月份更新
- sql azure笔记
- SQL Azure Reporting CTP
- SQL Azure 服务器端架构
- 一步一步 Sql Azure
- SQL Azure Federation Exercise
- SQL Azure Reporting
- SQL Azure是什么?
- SQL Azure Note
- SQL Azure Performance Tuning
- sql azure感受2
- Azure SQL Database 简介
- 源代码就是设计(Jack Reeves著)
- TextView属性详解
- Windows Phone Metro界面之我见
- eclipse快捷键收集整理
- 成为Java高手的25个学习目标
- SQL Azure Transactions
- 部分游戏开发工具
- Upstart: Ubuntu 的基于事件的启动进程
- 为什么移民加拿大
- HTML笔记(二)
- 2.EditText控件学习
- 2012年加拿大将扩大省提名份额
- ubuntu 和upstart启动过程研究
- gch文件之浅谈GCC预编译头技术