在Web Service中實現Transaction

來源:互聯網
上載者:User
web 在Web Service中實現Transaction
.Net Framework為類,WebForm和WebService提供了交易處理功能。
在傳統的windows應用程式中,要寫一個有交易處理功能的組件不僅要寫代碼而且要在元件服務中建立一個事務包。這就意味著在任何一台要處理這個事務的機器上,你都不得不開啟mmc在COM+應用程式節點下建立一個新包。
.NET Framework使得這一切變得很簡單,事實上我們不用在元件服務中作任何事,這一切都是自動完成的。對WebService來說,.NET Framework用Enterprise Services(COM+的替代者)來管理事務,而無需建立一個COM+包。所有管理事務狀態的工作都是在幕後完成的。
在webservice中實現這個很簡單。
1)在 [WebMethod()]屬性中指定transaction的類型。如[ WebMethod ( false, TransactionOption.RequiresNew) ]
以下是TransactionOption的詳細列表。
TransactionOption.Disabled    Ignore    any transaction in the current context.
TransactionOption.NotSupported    Create    the component in a context with no governing transaction.
TransactionOption.Supported    Share    a transaction if one exists; create a new transaction if necessary.
TransactionOption.Required    Create    the component with a new transaction, regardless of the state of the current context.
TransactionOption.RequiresNew    Share    a transaction if one exists.
2)用[AutoComplete]屬性確保Transaction能完成,除非拋出異常。
由此我們可以看出在Web Service中實現Transaction的一點特殊性,即Transaction屬性是應用於WebMethod上的。這意味著在webservice中只有設定了TransactionOption後才會應用事務。
注意:我們可以不要[AutoComplete],自己寫程式碼完成事務或中止事務,例子如下
try
{
   //Update the balances:
   //If an Account.Balance goes below 0,
   //an exception is thrown by the Account object
   _credit.Balance  = _actDB.getBalance ( _credit.ID );
   _debit.Balance  = _actDB.getBalance ( _debit.ID );
   ContextUtil.SetCommit;
}
//Catch the exception from the Account object
catch ( Exception ex )
{
   ContextUtil.SetAbort;
}


附上我的一段代碼:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.EnterpriseServices;

namespace Michael.WebServiceTrans
{
    public class FinancialUtil : System.Web.Services.WebService
    {
        //Create a class-level instance of the AccountDB class
        Michael.Data.AccountDB _actDB = new Michael.Data.AccountDB();
        
        
        [ WebMethod ( false, TransactionOption.RequiresNew ) ]
        [ AutoComplete ]
        public Decimal[] TransferMoney ( Decimal _amount,
                                                        String _fromActNum,
                                                        String _toActNum )
        {
            Account _debit = new Account ( _fromActNum );
            Account _credit = new Account ( _toActNum );
            
             Decimal[] _array = new Decimal[2];
            _actDB.debitOrCreditAccount ( true, _credit.ID, _amount);
            _actDB.debitOrCreditAccount ( false, _debit.ID, _amount );

            try
            {          _credit.Balance  = _actDB.getBalance ( _credit.ID );    
                _debit.Balance  = _actDB.getBalance ( _debit.ID );
            }
            catch ( Exception ex )
            {
                throw (new System.Exception ( ex.Message ) );
            }
            
            //Return the new balances in the array
            _array[0] = _debit.Balance;
            _array[1] = _credit.Balance;
            
            return _array;
        }

        
        [WebMethod()]
        public DataSet GetAllAccountNumbers ()
        {
            return _actDB.getAllAccountNums();
        }
        
        
//******************************************************//
//*********** VISUAL STUDIO DESIGNER CODE **************//
//******************************************************//
        
        public FinancialUtil()
        {
            InitializeComponent();
        }
        
        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }
        #endregion

        protected override void Dispose( bool disposing )
        {
        }

    }
}
  

相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。