.net 事务处理的三种方法

方法1:直接写入到sql 中web

在存储过程当中使用 BEGIN TRANS, COMMIT TRANS, ROLLBACK TRANS 实现sql

begin trans数据库

declare
@orderDetailsError int,
@procuntError int服务器

delete from [order details] where productid=42
select @orderDetailsError =@@error
delete from products where productid=42
select @procuntError=@@error分布式

if(@orderDetailsError =0 and @procuntError=0)
COMMIT TRANS
else
ROLLBACK TRANS性能

点:
全部事务逻辑包含在一个单独的调用中
拥有运行一个事务的最佳性能
独立于应用程序
限制:
事务上下文仅存在于数据库调用中
数据库代码与数据库系统有关
ui


方法2 :使用ADO.NET 实现 spa

使用ADO.NET 实现,使用这种方式的优势是能够在中间层来管理事务,固然你也能够选择在数据层来实现。
SqlConnection 和OleDbConnection 对象有一个 BeginTransaction 方法,它能够返回 SqlTransaction
或者OleDbTransaction 对象。并且这个对象有 Commit 和 Rollback 方法来管理事务
对象

SqlConnection sqlConnection = new SqlConnection("workstation id=WEIXIAOPING;packet size=4096;user id=sa;initial catalog=Northwind;persist security info=False");
   sqlConnection.Open();
   SqlTransaction myTrans = sqlConnection.BeginTransaction();
   SqlCommand sqlInsertCommand = new SqlCommand();
   sqlInsertCommand.Connection = sqlConnection
   sqlInsertCommand.Transaction=myTrans;
   try{
       sqlInsertCommand.CommandText="insert into tbTree(Context,ParentID) values('北京',1)";
       sqlInsertCommand.ExecuteNonQuery();
       sqlInsertCommand.CommandText="insert into tbTree(Context,ParentID) values('上海',1)";
       sqlInsertCommand.ExecuteNonQuery();
       myTrans.Commit();
     }catch(Exception ex)
     {
      myTrans.Rollback();
     }
    finally
    {
     sqlConnection.Close();
    }blog

优势:
     简单性
     和数据据事务差很少的快
     独立于数据库,不一样数据库的专有代码被隐藏了
缺点:
     事务不能跨越多个数据库链接
     事务执行在数据库链接层上,因此须要在事务过程当中维护一个数据库链接

     ADO.NET分布事务也能够跨越多个数据库,可是其中一个SQL SERVER 数据库的话,经过用SQL SERVER链接服务器链接到别的数据库,可是若是是在DB2和Orcal之间就不能够。


以上两种事务是常常用到的事务处理方法。

方法3 COM+事务(分布式事务)

.NET Framework 依靠 MTS/COM+ 服务来支持自动事务。COM+ 使用 Microsoft Distributed Transaction Coordinator (DTC) 做为事务管理器和事务协调器在分布式环境中运行事务。这样可以使.NET 应用程序运行跨多个资源结合不一样操做(例如,将定单插入 SQL Server 数据库、将消息写入 Microsoft 消息队列(MSMQ) 队列、以及从 Oracle 数据库检索数据)的事务。


   COM+事务处理的类必须继承System.EnterpriseServices.ServicedComponent,其实web service就是继承System.EnterpriseServices.ServicedComponent,因此web service也支持COM+事务。

定义一个COM+事务处理的类
[Transaction(TransactionOption.Required)]
public class DataAccess:System.EnterpriseServices.ServicedComponent
{}
TransactionOption枚举类型支持5个COM+值(Disabled,NotSupported,Required,RequiresNew,Supported)
Disabled      忽略当前上下文中的任何事务。
NotSupported 使用非受控事务在上下文中建立组件。
Required      若是事务存在则共享事务,而且若有必要则建立新事务。
RequiresNew   使用新事务建立组件,而与当前上下文的状态无关。
Supported     若是事务存在,则共享该事务。
通常来讲COM+中的组件须要Required 或Supported。当组件用于记录或查账时RequiresNew 颇有用,由于组件应该与活动中其余事务处理的提交或回滚隔离开来。
派生类能够重载基类的任意属性。如DataAccess选用Required,派生类仍然能够重载并指定RequiresNew或其余值。

COM+事务有手动处理和自动处理,自动处理就是在所须要自动处理的方法前加上[AutoComplete],根据方法的正常或抛出异常决定提交或回滚。
手动处理就是调用ContextUtil类中EnableCommit,SetComplete,SetAbort方法。

   public string testTransaction()
{
   try
   {
    ContextUtil.EnableCommit();
    InsertARecord1();
    InsertARecord2();
     ContextUtil.SetComplete();
    return "succeed!";
   }
   catch(Exception ex)
   {
           ContextUtil.SetAbort();
           return "failed!";
   }
}
      public void InsertARecord1()
{   
    string strconn="workstation id=WEIXIAOPING;packet size=4096;user id=sa;initial catalog=Northwind;persist security info=False";
    SqlConnection conn=new SqlConnection(strconn);
    conn.Open();
    SqlCommand command=new SqlCommand("insert into tbTree(Context,ParentID) values('北京',1)",conn);
    command.ExecuteNonQuery();
    conn.Close();
}
        public void InsertARecord2()
{
    string strconn="workstation id=WEIXIAOPING;packet size=4096;user id=sa;initial catalog=Northwind;persist security info=False";
    SqlConnection conn=new SqlConnection(strconn);
    conn.Open();
    SqlCommand command=new SqlCommand("insert into tbTree(Context,ParentID) values('上海',1)",conn);
    command.ExecuteNonQuery();
    conn.Close();
}
在须要事务跨 MSMQ 和其余可识别事务的资源(例如,SQL Server 数据库)运行的系统中,只能使用 DTC 或 COM+ 事务,除此以外没有其余选择。DTC 协调参与分布式事务的全部资源管理器,也管理与事务相关的操做。


缺点:
因为存在 DTC 和 COM 互操做性开销,致使性能下降。COM+事务处理的类必须强命名。

相关文章
相关标签/搜索