<%@ page contentType="text/html; charset=gb2312"%> 在Web Service中实现Transaction
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
在Web Service中实现Transaction
     发布者: 发布时间:2006-06-16
   在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 )
        {
        }

    }
}
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
根据客户端输入的文字生成图片,再传回给客户端的webservice
创建一个Windows Service应用程序
利用WebService技术实现远程数据库存取
发布一个webservice供大家使用
典型的Web Service结构
一步一步开始Web Service
 
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号