TransactionScope 的基本原理简介
C# 的事務(wù)編程
1 Db事務(wù)?
? DbConnection 中創(chuàng)建基于當前連接的 DbTransaction
?
2 ?使用TransactionScope ,創(chuàng)建環(huán)境事務(wù)
? 一旦創(chuàng)建,在這個環(huán)境包含的DbConnection 實例 都會根據(jù)連接字符串中的
Sqlserver 連接字符串支持,是否自動附加當前環(huán)境事務(wù).
連接字符串關(guān)鍵字(Enlist)
?????? SqlConnection.ConnectionString 屬性支持關(guān)鍵字 Enlist,該關(guān)鍵字指示 System.Data.SqlClient 是否將檢測事務(wù)上下文并自動在分布式事務(wù)中登記連接。 如果 Enlist=true,連接將自動在打開的線程的當前事務(wù)上下文中登記。 如果 Enlist=false,SqlClient 連接不會與分布式事務(wù)進行交互。 Enlist 的默認值為 true。 如果連接字符串中未指定 Enlist,若在連接打開時檢測到一個,連接將自動在分布式事務(wù)中登記。??
Server=(local)SQL2005;Database=Northwind;Integrated Security=SSPI;auto-enlist=false
Mysql 等其他 OLDP數(shù)據(jù)庫 需要驅(qū)動支持。
?
以下來自MSDN:
System.Transactions?基礎(chǔ)結(jié)構(gòu)提供了這兩個的顯式編程模型基于?Transaction?類,以及隱式編程模型使用?TransactionScope?類,在其中事務(wù)自動管理基礎(chǔ)結(jié)構(gòu)。
| 建議您創(chuàng)建使用隱式事務(wù)?TransactionScope?類,以便為您自動管理環(huán)境事務(wù)上下文。?您還應(yīng)該使用?TransactionScope?和?DependentTransaction?跨多個函數(shù)調(diào)用或多個線程調(diào)用需要使用相同的事務(wù)的應(yīng)用程序的類。?此模型的詳細信息,請參閱?Implementing An Implicit Transaction Using Transaction Scope?主題。?編寫事務(wù)應(yīng)用程序的詳細信息,請參閱?Writing A Transactional Application。 |
?
在實例化?TransactionScope?通過?new?語句中,事務(wù)管理器確定哪些事務(wù)參與進來。?一旦確定,該范圍將始終參與該事務(wù)。?此決策基于兩個因素:是否存在環(huán)境事務(wù)以及構(gòu)造函數(shù)中?TransactionScopeOption?參數(shù)的值。?環(huán)境事務(wù)是在代碼中執(zhí)行的事務(wù)。?可通過調(diào)用?Current?類的靜態(tài)?Transaction?屬性,獲取對環(huán)境事務(wù)的引用。?有關(guān)如何使用此參數(shù)的詳細信息,請參閱的"事務(wù)流的管理"部分?Implementing An Implicit Transaction Using Transaction Scope?主題。
如果在事務(wù)范圍內(nèi)未不發(fā)生任何異常 (即之間的初始化?TransactionScope?對象并調(diào)用其?Dispose?方法),則范圍所參與的事務(wù)可以繼續(xù)。?如果在事務(wù)范圍內(nèi)發(fā)生異常,參與到其中的事務(wù)將回滾。
當您的應(yīng)用程序完成所有工作時它想要在事務(wù)中執(zhí)行,應(yīng)調(diào)用?Complete?方法一次,以通知該事務(wù)管理器是可接受,即可提交事務(wù)。?未能調(diào)用此方法中止事務(wù)。
調(diào)用?Dispose?方法將標記事務(wù)范圍的末尾。?在調(diào)用此方法之后所發(fā)生的異常不會影響事務(wù)。
如果您修改的值?Current?內(nèi)某個范圍內(nèi),將引發(fā)異常時?Dispose?調(diào)用。?但是,在作用域結(jié)束時,以前的值被還原。?此外,如果您調(diào)用?Dispose?上?Current?在事務(wù)范圍創(chuàng)建事務(wù),事務(wù)將中止范圍的末尾。
?
// This function takes arguments for 2 connection strings and commands to create a transaction // involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the // transaction is rolled back. To test this code, you can connect to two different databases // on the same server by altering the connection string, or to another 3rd party RDBMS by // altering the code in the connection2 code block. static public int CreateTransactionScope(string connectString1, string connectString2,string commandText1, string commandText2) {// Initialize the return value to zero and create a StringWriter to display results.int returnValue = 0;System.IO.StringWriter writer = new System.IO.StringWriter();try{// Create the TransactionScope to execute the commands, guaranteeing// that both commands can commit or roll back as a single unit of work.using (TransactionScope scope = new TransactionScope()){using (SqlConnection connection1 = new SqlConnection(connectString1)){// Opening the connection automatically enlists it in the // TransactionScope as a lightweight transaction. connection1.Open();// Create the SqlCommand object and execute the first command.SqlCommand command1 = new SqlCommand(commandText1, connection1);returnValue = command1.ExecuteNonQuery();writer.WriteLine("Rows to be affected by command1: {0}", returnValue);// If you get here, this means that command1 succeeded. By nesting// the using block for connection2 inside that of connection1, you// conserve server and network resources as connection2 is opened// only when there is a chance that the transaction can commit. using (SqlConnection connection2 = new SqlConnection(connectString2)){// The transaction is escalated to a full distributed// transaction when connection2 is opened. connection2.Open();// Execute the second command in the second database.returnValue = 0;SqlCommand command2 = new SqlCommand(commandText2, connection2);returnValue = command2.ExecuteNonQuery();writer.WriteLine("Rows to be affected by command2: {0}", returnValue);}}// The Complete method commits the transaction. If an exception has been thrown,// Complete is not called and the transaction is rolled back. scope.Complete();}}catch (TransactionAbortedException ex){writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);}catch (ApplicationException ex){writer.WriteLine("ApplicationException Message: {0}", ex.Message);}// Display messages. Console.WriteLine(writer.ToString());return returnValue; }?
總結(jié)
以上是生活随笔為你收集整理的TransactionScope 的基本原理简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Magicodes.WeiChat——自
- 下一篇: 字符串计数