.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 )
{
}
}
}
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




