像TransactionScope一样使用DbTransaction

System.Transactions.TransactionScope为了提供一种非常方便的实现分布式事务的方式,但是在某些情况下为了阻止本地事务向分布式事务提升,我们只能通过DbTransaction进行事务控制。在进行数据库操作的时候,我们经常会遇到这样的事务场景:当某个方法单独执行的时候,它应该在一个单一的DbTransaction中执行;当多个方法一起执行的时候,它们应用共同在DbTransaction中执行。在本篇文章中我们通过对DbTransaction进行封装,提供一种类似于TransactionScope的编程方式来解决这个问题。[源代码从这里下载]

目录 一、自定义的Transaction 二、自定义CommittableTransaction和DependentTransaction 三、自定义TransactionScope 四、一个具有事务感知的DbHelper 五、三种事务控制的性能

一、自定义的Transaction

我们完全采用System.Transactions的应用编程接口的设计定义基于DbTransaction的相关类型,首选来看看具有如下定义的表示事务的Transaction类型。Transaction是一个抽象类,具有DbTransactionWrapper和Completed两个属性,前者表示对DbTransaction的封装,后者表示事务操作是否已经完成。静态属性Current表示当前事务,这是一个基于当前线程的静态字段。Rollback和Dispose直接调用了DbTransactionWrapper的同名方法。

   1: namespace Artech.Transactions
   2: {
   3:     public class DbTransactionWrapper: IDisposable
   4:     {
   5:         public DbTransactionWrapper(DbTransaction transaction)
   6:         {
   7:             this.DbTransaction = transaction;
   8:         }
   9:         public DbTransaction DbTransaction { get; private set; }
  10:         public bool IsRollBack { get; set; }
  11:         public void Rollback()
  12:         {
  13:             if (!this.IsRollBack)
  14:             {
  15:                 this.DbTransaction.Rollback();
  16:             }
  17:         }
  18:         public void Commit()
  19:         {
  20:             this.DbTransaction.Commit();
  21:         }
  22:         public void Dispose()
  23:         {
  24:             this.DbTransaction.Dispose();
  25:         }
  26:     }
  27:     public abstract class Transaction : IDisposable
  28:     {
  29:         [ThreadStatic]
  30:         private static Transaction current;
  31:  
  32:         public bool Completed { get; private set; }
  33:         public DbTransactionWrapper DbTransactionWrapper { get; protected set; }
  34:         protected Transaction() { }
  35:         public void Rollback()
  36:         {
  37:             this.DbTransactionWrapper.Rollback();
  38:         }
  39:         public DependentTransaction DependentClone()
  40:         {
  41:             return new DependentTransaction(this);
  42:         }
  43:         public void Dispose()
  44:         {
  45:             this.DbTransactionWrapper.Dispose();
  46:         }
  47:         public static Transaction Current
  48:         {
  49:             get { return current; }
  50:             set { current = value; }
  51:         }
  52:     }
  53: }

二、自定义CommittableTransaction和DependentTransaction

和定义在System.Transactions的事务一样,具体事务分为CommittableTransaction和DependentTransaction,前者可以使一个可以直接提交的独立事务,后者则是依附于某个事务的依赖事务。如下面的代码片断所示,我们直接基于某个DbTransaction来创建CommittableTransaction对象。而用于提交事务的Commit方法则直接调用DbTransaction的同名方法。

   1: public class CommittableTransaction : Transaction
   2: {
   3:     public CommittableTransaction(DbTransaction dbTransaction)
   4:     {
   5:         this.DbTransaction = dbTransaction;
   6:     }
   7:     public void Commit()
   8:     {
   9:         this.DbTransaction.Commit();
  10:     }
  11: }

下面是DependentTransaction的定义,我们直接基于某个作为内部事务的Transaction对象来创建DependentTransaction,而这个内部事务可以是一个CommittableTransaction,也可以是DependentTransaction。由于DependentTransaction只具有一个内部构造函数,所以DependentTransaction通过调用定义在Transaction的DependentClone方法来创建。

   1: public class DependentTransaction : Transaction
   2: {
   3:     public Transaction InnerTransaction { get; private set; }
   4:     internal DependentTransaction(Transaction innerTransaction)
   5:     {
   6:         this.InnerTransaction = innerTransaction;
   7:         this.DbTransaction = this.InnerTransaction.DbTransaction;
   8:     }
   9: }

三、自定义TransactionScope

我们在进行事务编程的时候只会使用到具有如下定义的TransactionScope类型。我们通过指定连接字符串名称、隔离级别以及用于创建DbProviderFactory的委托创建TransactionScope对象。在TransactionScope的构造函数中,如果通过Artech.Transactions.Transaction.Current属性表示的当前事务不存在,则根据DbProviderFactory创建DbConnection并调用BeginTransaction方法开启事务,并用被开启的DbTransaction创建CommittableTransaction对象。最终将创建的CommittableTransaction作为当前事务;服务过当前事务已经存在,则直接调用它的DependentClone方法创建的DependentTransaction作为当前事务。

   1: namespace Artech.Transactions
   2: {
   3:     public class TransactionScope: IDisposable
   4:     {
   5:         private Transaction transaction = Transaction.Current;
   6:         public bool Completed { get; private set; }
   7:  
   8:         public TransactionScope(string connectionStringName, IsolationLevel isolationLevel = IsolationLevel.Unspecified,
   9:             Func<string, DbProviderFactory> getFactory = null)
  10:         {
  11:             if (null == transaction)
  12:             {
  13:                 if (null == getFactory)
  14:                 {
  15:                     getFactory = cnnstringName => DbHelper.GetFactory(cnnstringName);
  16:                 }
  17:                 DbProviderFactory factory = getFactory(connectionStringName);
  18:                 DbConnection connection = factory.CreateConnection();
  19:                 connection.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
  20:                 connection.Open();
  21:                 DbTransaction dbTransaction = connection.BeginTransaction(isolationLevel);
  22:                 Transaction.Current = new CommittableTransaction(dbTransaction);
  23:             }
  24:             else
  25:             {
  26:                 Transaction.Current = transaction.DependentClone();
  27:             }           
  28:         }
  29:  
  30:         public void Complete()
  31:         {
  32:             this.Completed = true;
  33:         }
  34:         public void Dispose()
  35:         {
  36:             Transaction current = Transaction.Current;
  37:             Transaction.Current = transaction;
  38:             if (!this.Completed)
  39:             {
  40:                 current.Rollback();
  41:             }
  42:             CommittableTransaction committableTransaction = current as CommittableTransaction;
  43:             if (null != committableTransaction)
  44:             {
  45:                 if (this.Completed)
  46:                 {
  47:                     committableTransaction.Commit();
  48:                 }
  49:                 committableTransaction.Dispose();
  50:             }
  51:         }
  52:     }
  53: }

在事务操作完成之后必须调用Complete方法“提交事务”,此时我们将TransactionScope的Completed 属性设置为True。TransactionScope实现了IDisposable方法,Dispose方法具有这样的逻辑:先将当前状态还原成创建TransactionScope之初的状态,在还原之前先将当前事务保存下来。如果Completed属性为False,则直接调用Transaction的Rollback方法对事务进行回滚。如果Completed状态为True,而且当前事务为CommittableTransaction 则直接提交事务。

四、一个具有事务感知的DbHelper

为了演示通过捕捉当前事务来来控制具体事务的执行方式,我们写了如下一个DbHelper,其中ExecuteNonQuery用于在事务中执行指定的一段SQL。具体事务控制的逻辑是这样的:如果Artech.Transactions.Transaction.Current属性返回的事务存在,则将当前操作纳入封装的DbTransaction;如果System.Transactions.Transaction.Current属性返回的事务存在,操作的执行会自动纳入该事务中;如果上述两中环境事务均不存在,则创建一个单独的DbTransaction并将相应的操作纳入其中。

   1: public class DbHelper
   2: {
   3:     //其他成员
   4:     public int ExecuteNonQuery(string commandText, IDictionary<string, object> parameters)
   5:     {
   6:         DbConnection connection = null;
   7:         DbCommand command = this.DbProviderFactory.CreateCommand();
   8:         DbTransaction dbTransaction = null;
   9:         try
  10:         {
  11:             command.CommandText = commandText;
  12:             parameters = parameters ?? new Dictionary<string, object>();
  13:             foreach (var item in parameters)
  14:             {
  15:                 command.Parameters.Add(this.BuildDbParameter(item.Key, item.Value));
  16:             }
  17:             if (null != Artech.Transactions.Transaction.Current)
  18:             {
  19:                 command.Connection = Artech.Transactions.Transaction.Current.DbTransactionWrapper.DbTransaction.Connection;
  20:                 command.Transaction = Artech.Transactions.Transaction.Current.DbTransactionWrapper.DbTransaction;
  21:             }
  22:             else
  23:             {
  24:                 connection = this.DbProviderFactory.CreateConnection();
  25:                 connection.ConnectionString = this.ConnectionString;
  26:                 command.Connection = connection;
  27:                 connection.Open();
  28:                 if (System.Transactions.Transaction.Current == null)
  29:                 {
  30:                     dbTransaction = connection.BeginTransaction();
  31:                     command.Transaction = dbTransaction;
  32:                 }
  33:             }
  34:             int result = command.ExecuteNonQuery();
  35:             if (null != dbTransaction)
  36:             {
  37:                 dbTransaction.Commit();
  38:             }
  39:             return result;
  40:         }
  41:         catch
  42:         {
  43:             if (null != dbTransaction)
  44:             {
  45:                 dbTransaction.Rollback();
  46:             }
  47:             throw;
  48:         }
  49:         finally
  50:         {
  51:             if (null != connection)
  52:             {
  53:                 connection.Dispose();
  54:             }
  55:             if (null != dbTransaction)
  56:             {
  57:                 dbTransaction.Dispose();
  58:             }
  59:             command.Dispose();
  60:         }
  61:     }
  62: }

五、三种事务控制的性能

我们现在来测试批量操作在System.Transactions.TransactionScope、Artech.Transactions.TransactionScope和针对单独操作的DbTransaction的性能。我们在目标数据库中创建一个只包含Id和Name两个字段的数据表Users,并通过如下的CreateUser为该表添加一笔记录。

   1: static void CreateUser(string id, string name)
   2: {
   3:     string sql = "INSERT Users(ID, Name) Values(@id, @name)";
   4:     Dictionary<string, object> parameters = new Dictionary<string, object>();
   5:     parameters.Add("id", id);
   6:     parameters.Add("name", name);
   7:     DbHelper.ExecuteNonQuery(sql, parameters);
   8: }

而如下三个方法分别通过上述三种事务方式执行上面的这个方法,其中参数count为添加的数据量。

   1: private static void AddUsers1(int count)
   2: {
   3:     using (System.Transactions.TransactionScope transactionScope = new System.Transactions.TransactionScope())
   4:     {
   5:         for (int i = 0; i < count; i++)
   6:         {
   7:             CreateUser(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
   8:         }
   9:         transactionScope.Complete();
  10:     }
  11: }
  12:  
  13: private static void AddUsers2(int count)
  14: {
  15:     using (Artech.Transactions.TransactionScope transactionScope = new Artech.Transactions.TransactionScope("TestDb"))
  16:     {
  17:         for (int i = 0; i < count; i++)
  18:         {
  19:             CreateUser(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
  20:         }
  21:         transactionScope.Complete();
  22:     }
  23: }
  24:  
  25: private static void AddUsers3(int count)
  26: {
  27:     for (int i = 0; i < count; i++)
  28:     {
  29:         CreateUser(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
  30:     }
  31: }

为了剔除由于数据表现有数据量对数据访问性能的影响,我们在每次进行数据批量插入之前都回调用具有如下定义的DeleteAllUsers方法将表中的数据删除殆尽。

   1: private static void DeleteAllUsers()
   2: {
   3:     string sql = "DELETE USERS";
   4:     DbHelper.ExecuteNonQuery(sql, null);
   5: }

下面是我们最终的测试程序,我们分别测试了数据量为100、1,000、10,000和100,000的数据写入操作分别在上述三种事务控制方式的耗时。

   1: Stopwatch stopWatch = new Stopwatch();
   2:  
   3: //100
   4: Console.WriteLine("100");
   5: DeleteAllUsers();
   6: stopWatch.Restart();
   7: AddUsers1(100);
   8: Console.WriteLine("{0, -36}: {1}", "System.Transactions.TransactionScope", stopWatch.ElapsedMilliseconds);
   9:  
  10: DeleteAllUsers();
  11: stopWatch.Restart();
  12: AddUsers2(100);
  13: Console.WriteLine("{0, -36}: {1}", "Artech.Transactions.TransactionScope", stopWatch.ElapsedMilliseconds);
  14:  
  15: DeleteAllUsers();
  16: stopWatch.Restart();
  17: AddUsers3(100);
  18: Console.WriteLine("{0, -36}: {1}\n", "Single Transnaction", stopWatch.ElapsedMilliseconds);
  19:  
  20: //1000
  21: Console.WriteLine("1000");
  22: DeleteAllUsers();
  23: stopWatch.Restart();
  24: AddUsers1(1000);
  25: Console.WriteLine("{0, -36}: {1}", "System.Transactions.TransactionScope", stopWatch.ElapsedMilliseconds);
  26:  
  27: DeleteAllUsers();
  28: stopWatch.Restart();
  29: AddUsers2(1000);
  30: Console.WriteLine("{0, -36}: {1}", "Artech.Transactions.TransactionScope", stopWatch.ElapsedMilliseconds);
  31:  
  32: DeleteAllUsers();
  33: stopWatch.Restart();
  34: AddUsers3(1000);
  35: Console.WriteLine("{0, -36}: {1}\n", "Single Transnaction", stopWatch.ElapsedMilliseconds);
  36:  
  37: //10000
  38: Console.WriteLine("10000");
  39: DeleteAllUsers();
  40: stopWatch.Restart();
  41: AddUsers1(10000);
  42: Console.WriteLine("{0, -36}: {1}", "System.Transactions.TransactionScope", stopWatch.ElapsedMilliseconds);
  43:  
  44: DeleteAllUsers();
  45: stopWatch.Restart();
  46: AddUsers2(10000);
  47: Console.WriteLine("{0, -36}: {1}", "Artech.Transactions.TransactionScope", stopWatch.ElapsedMilliseconds);
  48:  
  49: DeleteAllUsers();
  50: stopWatch.Restart();
  51: AddUsers3(10000);
  52: Console.WriteLine("{0, -36}: {1}\n", "Single Transnaction", stopWatch.ElapsedMilliseconds);
  53:  
  54: //100000
  55: Console.WriteLine("100000");
  56: DeleteAllUsers();
  57: stopWatch.Restart();
  58: AddUsers1(100000);
  59: Console.WriteLine("{0, -36}: {1}", "System.Transactions.TransactionScope", stopWatch.ElapsedMilliseconds);
  60:  
  61: DeleteAllUsers();
  62: stopWatch.Restart();
  63: AddUsers2(100000);
  64: Console.WriteLine("{0, -36}: {1}", "Artech.Transactions.TransactionScope", stopWatch.ElapsedMilliseconds);
  65:  
  66: DeleteAllUsers();
  67: stopWatch.Restart();
  68: AddUsers3(100000);
  69: Console.WriteLine("{0, -36}: {1}", "Single Transaction", stopWatch.ElapsedMilliseconds);

下面是测试程序输出结果:

   1: 100
   2: System.Transactions.TransactionScope: 28
   3: Artech.Transactions.TransactionScope: 11
   4: Single Transnaction                 : 57
   5:  
   6: 1000
   7: System.Transactions.TransactionScope: 140
   8: Artech.Transactions.TransactionScope: 83
   9: Single Transnaction                 : 469
  10:  
  11: 10000
  12: System.Transactions.TransactionScope: 1530
  13: Artech.Transactions.TransactionScope: 826
  14: Single Transnaction                 : 4659
  15:  
  16: 100000
  17: System.Transactions.TransactionScope: 13562
  18: Artech.Transactions.TransactionScope: 8346
  19: Single Transaction                  : 48277

上面的输出结果表明我们自定义的TransactionScope具有最好的性能,不过优势还不是很明显,这是由于连接池机制的原因。现在我们通过如下的方式将连接字符创对连接池的支持关闭:

   1: <configuration>
   2:   <connectionStrings>
   3:     <add name="TestDb" 
   4:          providerName="System.Data.SqlClient" 
   5:          connectionString="Data Source=.;Initial Catalog=TestDb;Integrated Security=True; Pooling=False;"/>
   6:   </connectionStrings>
   7: </configuration>

再次运行我们的程序我们就会看到三种事务处理方式在性能上的显著差异(当数据量为100,000时,采用System.Transactions.TransactionScope直接就崩溃了)

   1: 100
   2: System.Transactions.TransactionScope: 2318
   3: Artech.Transactions.TransactionScope: 13
   4: Single Transnaction                 : 310
   5:  
   6: 1000
   7: System.Transactions.TransactionScope: 2949
   8: Artech.Transactions.TransactionScope: 124
   9: Single Transnaction                 : 3623
  10:  
  11: 10000
  12: System.Transactions.TransactionScope: 32754
  13: Artech.Transactions.TransactionScope: 1213
  14: Single Transnaction                 : 30631
  15:  
  16: 100000
  17:  
  18: 未经处理的异常:  System.Data.SqlClient.SqlException: Import of Microsoft Distributed Transaction Coordinator (MS DTC) transaction failed:...

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏大内老A

我的WCF之旅(5):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的重载(Overloading)

对于.NET重载(Overloading)——定义不同参数列表的同名方法(顺便提一下,我们但可以在参数列表上重载方法,我们甚至可以在返回类型层面来重载我们需要的...

29160
来自专栏Java架构沉思录

Redisson是如何实现分布式锁的?

针对项目中使用的分布式锁进行简单的示例配置以及源码解析,并列举源码中使用到的一些基础知识点,但是没有对redisson中使用到的netty知识进行解析。

19630
来自专栏GreenLeaves

Asp.Net webconfig中使用configSections的用法

  最近闲来无事,研究研究公司的框架,无意中打开了webconfig页面,发现了一个我不认识的节点<configSections></configSection...

218100
来自专栏大内老A

[WCF REST] WebServiceHost有何特别之处?

WCF为REST服务的寄宿提供了一个新的ServiceHost,即WebServiceHost。WebServiceHost是ServiceHost的子类,而W...

22660
来自专栏王磊的博客

linq to entity常用操作

一、聚合函数查询 double sum = 0; using (xxxEntities db = new xxxEntities()) { sum = ...

38360
来自专栏Create Sun

基础拾遗-----数据注解与验证

前言 其实对于这块知识点,一直觉得没有必要进行总结,只是新到的公司当时用到了 kendo for asp.net mvc ,里面有用到,自动初始化页面,而依据基...

337100
来自专栏逸鹏说道

c# 温故而知新: 线程篇(一) 下

Abort 方法: 其实 Abort 方法并没有像字面上的那么简单,释放并终止调用线程,其实当一个线程调用 Abort方法时,会在调用此方法的线程上引发一个异常...

27060
来自专栏.NET技术

.net平台的MongoDB使用

  最近花了点时间玩了下MongoDB.Driver,进行封装了工具库,平常也会经常用到MongoDB,因此写一篇文章梳理知识同时把自己的成果分享给大家。

15520
来自专栏待你如初见

Java爬虫及分布式部署

36150
来自专栏跟着阿笨一起玩NET

关于DataGridView_DataError事件的问题

本文转载:http://blog.csdn.net/szstephenzhou/article/details/7834725

47610

扫码关注云+社区

领取腾讯云代金券