1. 程式人生 > 其它 >C#中使用TransactionScope類(分散式事務)

C#中使用TransactionScope類(分散式事務)

技術標籤:資料庫分散式javaspringmysql

如果在C#中使用TransactionScope類(分散式事務),則須注意如下事項:
1、在專案中引用using System.Transactions名稱空間(先要在新增net元件的引用);

2、具體示例如下:

        public static void sendMessage()
        {           
            TransactionOptions transactionOption = new TransactionOptions();

            //設定事務隔離級別
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

            // 設定事務超時時間為60秒
            transactionOption.Timeout = new TimeSpan(0, 0, 60);

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption))
            {
                try
                {
                    //TODO
                    scope.Complete();
                }
                catch (Exception ex) {
                    throw new Exception(ex.Message);
                }finally{
                    //釋放資源
                    scope.Dispose();
                  }                               
            }
        }
namespace System.Transactions
{
    //
    // Summary:
    //     Specifies the isolation level of a transaction.
    public enum IsolationLevel
    {
        //
        // Summary:
        //     Volatile data can be read but not modified, and no new data can be added during the transaction.可讀取但不可修改易失性資料,並且在此過程中不能新增新資料交易。
        Serializable = 0,
        //
        // Summary:
        //     Volatile data can be read but not modified during the transaction. New data can be added during the transaction.在事務期間可以讀取但不能修改易失性資料。新資料可以在事務處理期間新增。
        RepeatableRead = 1,
        //
        // Summary:
        //     Volatile data cannot be read during the transaction, but can be modified.事務期間無法讀取易失性資料,但可以對其進行修改。
        ReadCommitted = 2,
        //
        // Summary:
        //     Volatile data can be read and modified during the transaction.在事務期間可以讀取和修改易失性資料。
        ReadUncommitted = 3,
        //
        // Summary:
        //     Volatile data can be read. Before a transaction modifies data, it verifies if
        //     another transaction has changed the data after it was initially read. If the
        //     data has been updated, an error is raised. This allows a transaction to get to
        //     the previously committed value of the data.可以讀取易失性資料。在事務修改資料之前,它會驗證另一個事務在最初讀取資料後更改了資料。如果資料已更新,出現錯誤。這使得事務可以到達以前提交的資料值。
        Snapshot = 4,
        //
        // Summary:
        //     The pending changes from more highly isolated transactions cannot be overwritten.無法覆蓋來自高度隔離事務的掛起更改。
        Chaos = 5,
        //
        // Summary:
        //     A different isolation level than the one specified is being used, but the level cannot be determined. An exception is thrown if this value is set.正在使用與指定隔離級別不同的隔離級別,但無法確定。如果設定了此值,則會引發異常。
        Unspecified = 6
    }
}

事務五種隔離級別IsolationLevel屬性一共支援五種事務設定,具體介紹如下:
(1)DEFAULT
  使用資料庫設定的隔離級別(預設),由DBA 預設的設定來決定隔離級別。
(2)READ_UNCOMMITTED
  這是事務最低的隔離級別,它充許別外一個事務可以看到這個事務未提交的資料。
  會出現髒讀、不可重複讀、幻讀 (隔離級別最低,併發效能高)。
(3)READ_COMMITTED
  保證一個事務修改的資料提交後才能被另外一個事務讀取。另外一個事務不能讀取該事務未提交的資料。
  可以避免髒讀,但會出現不可重複讀、幻讀問題(鎖定正在讀取的行)。
(4)REPEATABLE_READ
  可以防止髒讀、不可重複讀,但會出幻讀(鎖定所讀取的所有行)。

(5)SERIALIZABLE
  這是花費最高代價但是最可靠的事務隔離級別,事務被處理為順序執行。
  保證所有的情況不會發生(鎖表)。

3、對MSDTC元件設定:
在控制面板--->管理工具--->服務 中,開啟Distributed Transaction Coordinator 服務。
a.控制面板->管理工具->元件服務->計算機->我的電腦->右鍵->屬性
b.選擇MSDTC頁, 確認"使用本地協調器"
c.點選下方"安全配置"按鈕
d.勾選: "允許網路DTC訪問","允許遠端客戶端","允許入站","允許出站","不要求進行身份驗證".
e.對於資料庫伺服器端, 可選擇"要求對呼叫方驗證"
f.勾選:"啟用事務Internet協議(TIP)事務"。
g.在雙方防火牆中增加MSDTC.exe例外
可用命令列: netsh firewall set allowedprogram %windir%\system32\msdtc.exe MSDTC enable

4、重啟IIS伺服器。

參考

  TransactionScop事務機制的使用