事务处理通常情况是用于数据库的,也可以基于内存中的对象执行事务处理,如一列对象,对于支持事务处理的一列对象,如果添加或者删除了其中一个对象时事务处理失败,这个列表的操作会自动撤销。

事务处理的最常见得用途是写入或者更新数据库中的数据。在消息队列中写入消息,或将数据写入文件中等也可以使用事务的。

每个事务处理通常有以下四种特性:

Atomicity(原子性)

事务必须是原子工作单元;对于其数据修改,要么全都执行,要么全都不执行。通常,与某个事务关联的操作具有共同的目标,并且是相互依赖的。如果系统只执行这些操作的一个子集,则可能会破坏事务的总体目标。原子性消除了系统处理操作子集的可能性。 

Consistency(一致性)

事务在完成时,必须使所有的数据都保持一致状态。在相关数据库中,所有规则都必须应用于事务的修改,以保持所有数据的完整性。事务结束时,所有的内部数据结构(如 B 树索引或双向链表)都必须是正确的。某些维护一致性的责任由应用程序开发人员承担,他们必须确保应用程序已强制所有已知的完整性约束。例如,当开发用于转帐的应用程序时,应避免在转帐过程中任意移动小数点。 

Isolation(隔离性)

 由并发事务所作的修改必须与任何其它并发事务所作的修改隔离。事务查看数据时数据所处的状态,要么是另一并发事务修改它之前的状态,要么是另一事务修改它之后的状态,事务不会查看中间状态的数据。这称为可串行性,因为它能够重新装载起始数据,并且重播一系列事务,以使数据结束时的状态与原始事务执行的状态相同。当事务可序列化时将获得最高的隔离级别。在此级别上,从一组可并行执行的事务获得的结果与通过连续运行每个事务所获得的结果相同。由于高度隔离会限制可并行执行的事务数,所以一些应用程序降低隔离级别以换取更大的吞吐量。

Durability(持久性)

 事务完成之后,它对于系统的影响是永久性的。该修改即使出现致命的系统故障也将一直保持。

 以上介绍了什么叫做事务处理下面开始步入正题

 在发布System.Transactions命名空间之前,可以直接使用ADO.net创建事务处理,也可以使用通过组建,属性和com+运行库(System.EnterpriseServices)进行事务处理。那么也就可以讲一共有三种事务处理方式:ADO.net事务处理、企业服务事务处理、System.Transactions事务处理。

 1.ADO.net事务处理

 这个可以说是最为传统的事务处理,如果没有手工创建事务处理,每个sql语句将都有一个事务处理。如果多个语句参加到一个事务处理中,就必须手工创建一个事务处理。如果有多个语句运行在同一个事务处理中,每个语句必须与同一个连接关联起来。也就是ADO.net事务处理不支持跨多个连接的事务处理,它总是关联到一个连接上的本地事务处理(ADO.net事务处理不是分布式处理,很难让多个对象在同一个事务处理工作)。

ado事务处理 
   class Program
    {
        
//使用ado.net事务处理
        static void Main()
        {
            SqlConnection connection 
= new SqlConnection("server=(local);database=CourseManagement;trusted_connection=true");
            SqlCommand courseCommand 
= connection.CreateCommand();
            courseCommand.CommandText 
= "INSERT INTO Courses (Number, Title) VALUES (@Number, @Title)";
            connection.Open();
            SqlTransaction tx 
= connection.BeginTransaction();

            
try
            {
                courseCommand.Transaction 
= tx;

                courseCommand.Parameters.AddWithValue(
"@Number""2124");
                courseCommand.Parameters.AddWithValue(
"@Title""C# Programming");
                
int rowResult = courseCommand.ExecuteNonQuery();
                
if (rowResult != 1)
                {
                    
throw new Exception("ex");
                }

                tx.Commit();
            }
            
catch (Exception ex)
            {
                Console.WriteLine(
"error: {0}", ex.Message);
                tx.Rollback();
            }
            connection.Close();
 
        }

2. 企业服务事务处理

该事务处理的最大优点是,能够不在明确进行事务处理,运行库会自动创建事务处理。只需要该有事务处理需求的类添加[Transaction]特性即可以及继承ServicedComponent类。另外一个优点是多个对象能轻松运行在同一个事务处理中,事务处理可以自动注册,缺点就是使用这个技术的类需要继承ServicedComponent。

企业服务事务处理 
 
public class CourseData1:ServicedComponent
    {
        [AutoComplete]
        
public void AddCourse(Course c)
        {
            SqlConnection connection 
= new SqlConnection(Properties.Settings.Default.CourseManagementConnectionString);
            SqlCommand courseCommand 
= connection.CreateCommand();
            courseCommand.CommandText 
= "INSERT INTO Courses (Number, Title) VALUES (@Number, @Title)";
            connection.Open();
          

            
try
            {
                courseCommand.Transaction 
= tx;

                courseCommand.Parameters.AddWithValue(
"@Number", c.Number);
                courseCommand.Parameters.AddWithValue(
"@Title", c.Title);
                
int rowResult = courseCommand.ExecuteNonQuery();
                
if (rowResult != 1)
                {
                    
throw new Exception("ex");
                }

              
            }
            
catch (Exception ex)
            {
                Console.WriteLine(
"error: {0}", ex.Message);
             
                Trace.WriteLine(
"Error: " + ex.Message);
            }
            connection.Close();

        }

3.使用System.Transactions

    可提交的事务处理 :通过使用Transactions子类唯一支持提交事务处理类得CommittableTransaction

AddStudent
   #region AddStudent with a transaction
      
public void AddStudent(Student student, Transaction tx)
      {
         SqlConnection connection 
= new SqlConnection(
             
"server=GSKENING-CEA635\\SQLEXPRESS;database=CourseManagement;trusted_connection=true;User=sa;Password=123456");
         connection.Open();
         
try
         {
            
if (tx != null)
               connection.EnlistTransaction(tx);
//登记对象
            SqlCommand command = connection.CreateCommand();

            command.CommandText 
= "INSERT INTO Students (Firstname, Lastname, Company) VALUES (@Firstname, @Lastname, @Company)";
            command.Parameters.AddWithValue(
"@Firstname", student.FirstName);
            command.Parameters.AddWithValue(
"@Lastname", student.LastName);
            command.Parameters.AddWithValue(
"@Company", student.Company);

            command.ExecuteNonQuery();
         }
         
finally
         {
            connection.Close();
         }

      }
      
#endregion

 

main
      static void Main()
        {
            CommittableTransaction tx 
= new CommittableTransaction();
            Utilities.DisplayTransactionInformation(
"TX created", tx.TransactionInformation);

            
try
            {

                Student s1 
= new Student();
                s1.FirstName 
= "Neno";
                s1.LastName 
= "Loye";
                s1.Company 
= "thinktecture";
                StudentData db 
= new StudentData();
                db.AddStudent(s1, tx);

           

                
if (Utilities.AbortTx())
                {
                    
throw new ApplicationException("transaction abort");
                }

                tx.Commit();
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine();
                tx.Rollback();
            }

            Utilities.DisplayTransactionInformation(
"TX completed", tx.TransactionInformation);
            
        }
    }

  事务处理升级:就是本机参与事务处理的资源,会创建本地的事务或分布式事务处理。如果多个资源添加到事务处理中,事务处理就可以先设置为本地,后再根据需要升级为分布式事务来处理(分布式事务处理需要启动分布式事务处理协调器DTC)。当多个连接添加到事务处理中时,同样会经行这种升级。下面代码是修改上面讲的可提交的事务处理。

main
     static void Main()
        {
            CommittableTransaction tx 
= new CommittableTransaction();
            Utilities.DisplayTransactionInformation(
"TX created", tx.TransactionInformation);

            
try
            {

                Student s1 
= new Student();
                s1.FirstName 
= "Neno";
                s1.LastName 
= "Loye";
                s1.Company 
= "thinktecture";
                StudentData db 
= new StudentData();
                db.AddStudent(s1, tx);


                Student s2 
= new Student();
                s2.FirstName 
= "Dominick";
                s2.LastName 
= "Baier";
                s2.Company 
= "thinktecture";
                db.AddStudent(s2, tx);

                Utilities.DisplayTransactionInformation(
"2nd connection enlisted", tx.TransactionInformation);

             

                
if (Utilities.AbortTx())
                {
                    
throw new ApplicationException("transaction abort");
                }

                tx.Commit();
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine();
                tx.Rollback();
            }

            Utilities.DisplayTransactionInformation(
"TX completed", tx.TransactionInformation);
            
        }

 环境事务处理:

使用环境事务处理,就不需要手动登记与事务的连接;在支持环境事务处理的资源中,自动实现的

 

环境事务处理
 class Program
    {
        
static void Main()
        {
           
            
using (TransactionScope scope = new TransactionScope())
            {
                Transaction.Current.TransactionCompleted 
+= OnTransactionCompleted;

                Utilities.DisplayTransactionInformation(
                    
"Ambient TX created",
                    Transaction.Current.TransactionInformation);

                Student s1 
= new Student();
         
                s1.FirstName 
= "Ingo";
                s1.LastName 
= "Rammer";
                s1.Company 
= "thinktecture";
                StudentData db 
= new StudentData();
                db.AddStudent(s1);

                
//using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew))
                
//{
                
//    Transaction.Current.TransactionCompleted += OnTransactionCompleted;

                
//    Utilities.DisplayTransactionInformation(
                
//        "Inner Transaction Scope",
                
//        Transaction.Current.TransactionInformation);

                
//    scope2.Complete();
                
//}
               

                
if (!Utilities.AbortTx())
                    scope.Complete();
                
else
                    Console.WriteLine(
"abort transaction");
            }
        }

        
static void OnTransactionCompleted(object sender, TransactionEventArgs e)
        {
           Utilities.DisplayTransactionInformation(
"transaction completed",
              e.Transaction.TransactionInformation);
        }
    }

 代码下载/Files/ylwn817/Transactions.rar

  

posted on 2011-05-12 08:58  jackdesk  阅读(985)  评论(1编辑  收藏  举报