三種交易處理 交易處理是在資料處理時經常遇到的問題,經常用到的方法有以下3種總結整理如下: 方法1:直接寫入到sql 中 在預存程序中使用 BEGIN TRANS, COMMIT TRANS, ROLLBACK TRANS 實現 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 優點: 所有事務邏輯包含在一個單獨的調用中 擁有運行一個事務的最佳效能 獨立於應用程式 限制: 事務上下文僅存在於資料庫調用中 資料庫代碼與資料庫系統有關 方法2 :使用ADO.Net 實現 使用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(); } 優點: 簡單性 和資料據事務差不多的快 獨立於資料庫,不同資料庫的專有代碼被隱藏了 缺點: 事務不能跨越多個資料庫連接 事務執行在資料庫連接層上,所以需要在事務過程中維護一個資料庫連接 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 Interop性開銷,導致效能降低。 COM+交易處理的類必須強命名。 |