浏览器大全:是一个提供流行浏览器教程、在线学习分享的学习平台!

在Web Service中完成Transaction

在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.DisabledIgnoreany transaction in the current context.
TransactionOption.NotSupportedCreatethe component in a context with no governing transaction.
TransactionOption.SupportedSharea transaction if one exists; create a new transaction if necessary.
TransactionOption.RequiredCreatethe component with a new transaction, regardless of the state of the current context.
TransactionOption.RequiresNewSharea 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 )
{
}

}
}


相关软件

2345加速浏览器官方版

2345加速浏览器官方版 | 56.2MB

2345加速浏览器官方版

新一代2345加速浏览器采用Chromium和IE双内核,主打极速与安全特性。基于Chromium深度定制,引入网页智能预加载技术,访问网页更快速..

QQ浏览器官方正式版

QQ浏览器官方正式版 | 49.67MB

QQ浏览器官方正式版

QQ浏览器秉承TT浏览器1-4系列方便易用的特点,但技术架构不同,交互和视觉表现也重新设计,采用Chromium内核+IE双内核,让浏览快速稳定...

百度浏览器最新版下载

百度浏览器最新版下载 | 13.3MB

百度浏览器最新版下载

q百度浏览器,是一款简洁轻快、智能懂你的浏览器。依靠百度强大的搜索平台,在满足用户浏览网页的基础上,它整合百度体系业务优势,带给用户更方便的浏览方式功能...

UC浏览器官方正式版

UC浏览器官方正式版 | 44.2MB

UC浏览器官方正式版

UC浏览器(UC Browser)是UC Mobile Limited在2004年8月开发的一款软件,分uc手机浏览器和uc浏览器电脑版。UC浏览器是全球使用量最大的第三方手机浏览器...

猎豹浏览器2022最新版下载

猎豹浏览器2022下载 | 45MB

猎豹浏览器2022最新版下载

猎豹安全浏览器对Chrome的Webkit内核进行了超过100项的技术优化,访问网页速度更快。其具有首创的智能切换引擎,动态选择内核匹配不同网页...

360安全浏览器官方版下载

360安全浏览器下载 | 21.4MB

360安全浏览器官方版下载

360安全浏览器拥有全国最大的恶意网址库,采用恶意网址拦截技术,可自动拦截挂马、欺诈、网银仿冒等恶意网址。独创沙箱技术,在隔离模式即使访问****也不会感染...