什么是 System.Transactions(msdn)?

System.Transactions 是 .NET 2.0 框架中新增的事务控件命名空间。它是一种处理分布式事务的新方式,没有 COM+ 注册和 COM+ 目录的开销。请注意,Microsoft 分布式事务协调器用于初始化事务。

运行情况

同步定单处理中的 Order.Insert() 方法使用 System.Transactions 插入一个定单并更新库存。通过添加对 System.Transaction 命名空间的引用,并将定单插入方法和库存减少方法包装在 TransactionScope 内,我们实现了 Order.Insert() 方法,如代码清单 1 所示。

清单 1. 运行中的 System.Transactions

using System;
using System.Transactions;
using PetShop.IBLLStrategy;

namespace PetShop.BLL {
    /// <summary>
    /// This is a synchronous implementation of IOrderStrategy 
    /// By implementing IOrderStrategy interface, the developer can 
    /// add a new order insert strategy without re-compiling the whole 
    /// BLL.
    /// </summary>
    public class OrderSynchronous : IOrderStrategy {
      ...
        /// <summary>
        /// Inserts the order and updates the inventory stock within 
        /// a transaction.
        /// </summary>
        /// <param name="order">All information about the order</param>
        public void Insert(PetShop.Model.OrderInfo order) {

            using (TransactionScope ts = new                      
TransactionScope(TransactionScopeOption.Required)) {

                dal.Insert(order);

                // Update the inventory to reflect the current inventory 
                // after the order submission.
                Inventory inventory = new Inventory();
                inventory.TakeStock(order.LineItems);

                // Calling Complete commits the transaction.
                // Excluding this call by the end of TransactionScope's
                // scope will rollback the transaction.
                ts.Complete();
            }
        }
    }
}

在 .NET Pet Shop 3 中,分布式事务由企业服务处理,需要 COM+ 注册。OrderInsert 类从服务组件派生,事务由 COM+ 处理。然后,服务组件使用 regsvr32 命令进行注册。

清单 2. Pet Shop 3 的定单插入

using System;
using System.Collections;
using System.EnterpriseServices;
using System.Runtime.InteropServices;
...
namespace PetShop.BLL {
   /// <summary>
   /// A business component to manage the creation of orders
   /// Creation of an order requires a distributed transaction
   /// so the Order class derives from ServicedComponents
   /// </summary>
   [Transaction(System.EnterpriseServices.TransactionOption.Required)]
   [ClassInterface(ClassInterfaceType.AutoDispatch)]
   [ObjectPooling(MinPoolSize=4, MaxPoolSize=4)]
   [Guid("14E3573D-78C8-4220-9649-BA490DB7B78D")]
   public class OrderInsert : ServicedComponent {
      ...
      /// <summary>
      /// A method to insert a new order into the system
      /// The orderId will be generated within the method and should not
      /// be supplied as part of the order creation the inventory will be 
      /// reduced by the quantity ordered.
      /// </summary>
      /// <param name="order">All the information about the order</param>
      /// <returns>
      /// The new orderId is returned in the order object
      /// </returns>
      [AutoComplete]
      public int Insert(OrderInfo order) {

         // Get an instance of the Order DAL using the DALFactory
         IOrder dal = PetShop.DALFactory.Order.Create();

         // Call the insert method in the DAL to insert the header
         int orderId = dal.Insert(order);

         // Get an instance of the Inventory business component
         Inventory inventory = new Inventory();
         inventory.TakeStock( order.LineItems);
         ...

         // Set the orderId so that it can be returned to the caller
         return orderId;
      }
   }
}

System.Transactions 的优点

从企业服务移动到 System.Transactions 可以简化部署,因为后者不需要使用 COM+ 目录。使用 COM+ 目录时,我们忽略了其他一些额外的功能,只保留了分布式事务支持。System.Transaction 使得在 ASP.NET 2.0 应用程序中编程和部署分布式应用程序变得十分简单。System.Transactions 在运行时的性能提高了 50%,因为它避免了对象实例化的 COM+ 目录查找所产生的开销。最后一个优点是,针对 SQL Server 2005 运行时,System.Transactions 能够检测到某个分布式事务何时针对宿主在一个 SQL Server 2005 实例上的两个不同数据库运行。在这种情况下,它能够将该分布式事务提升为一个本地事务,这样就可避免与分布式事务登录/两阶段提交相关的全部开销,从而极大地提高性能。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值