SQL Azure Transactions

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();
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值