为什么交易不会回滚Web服务和数据库之间? [英] Why transaction not be rolled back between a web service and database?

查看:177
本文介绍了为什么交易不会回滚Web服务和数据库之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 System.Transactions的围绕Web服务和数据库。本次交易不回滚时,我无法连接到远程数据库。因此数据被插入在Web服务的数据库,而应被回滚。我写的Web服务code我的自我。 Distributed Transaction Coordinator服务在运行,我设置在组件服务等参数。

I'm using system.Transactions around a web service and a database. The transaction doesn't roll back when I can't connect to remote database. so the data is inserted in the database of web service while should be rolled back. I'm writing web service code my self. Distributed transaction coordinator service is run and I set the other parameters in Component Services.

这是更好地说,这两个Web服务和客户端在同一台机器。

It's better to say both Web service and client are in the same machine.

using System.Transactions;

 try
{
    using (TransactionScope tscope = new TransactionScope())
    {
        //----- Web service
        IdentificationSystem.Service Identify = new IdentificationSystem.Service();
        Identify.InsertWorkshopInfo(BosWorkshop.WpSvUserName,          BosWorkshop.WpSvPassword,BosWorkshop.WkIcode
        //-------------
        //------------------ connect to a remote DB
        //--------- My Sql Query
        string sq= "SET XACT_ABORT ON;update tbl_kargah " +
            "set email_kargah=email_kargah+@WpCode "+
            "where co_kargah=@co_kargah "+
            "insert into dbo.tbl_WorkshopCodes (co_kargah,Code_ostan,Wpcode) "+
            "values(@Co_kargah,@Code_ostan,@WpCode) "+
            "insert into LnkWorkerSystem.Bazresi_kar.dbo.tbl_Workshopcodes(Co_Kargah,Code_Ostan,WpCode) "+
            "values(@Co_kargah,@Code_ostan,@WpCode) ";

        //--------
        SqlCon = new SqlConnection(@BoBaz.BazConnectionString);
        SqlCon.Open();
        SqlCom = new SqlCommand(sq, SqlCon);
        SqlCom.Parameters.Add("@co_kargah", SqlDbType.BigInt).Value = BoBaz.BazCodeKargah;
        SqlCom.Parameters.Add("@Code_ostan", SqlDbType.NVarChar).Value = BoBaz.BazCodeOstan;
        SqlCom.Parameters.Add("@WpCode", SqlDbType.NChar).Value = BoBaz.WpRealCode;
        SqlCom.ExecuteNonQuery();
        SqlCon.Close();

        tscope.Complete();
    }
}
catch(Exception ex)
{
    string s = ex.Message;
}

请参考此问题,请:<一href=\"http://stackoverflow.com/questions/6640538/is-transaction-between-a-web-service-and-a-database-possible\">Transaction

推荐答案

编辑:

在很多对话,这将您正在使用ASMX Web服务看起来。这些都是 基本网络服务,并且不支持这种用法。

After lots of dialog, it would seem you are using an ASMX web service. These are basic web-services, and do not support this usage.

增加对这种支持是由之类的东西WCF的在特定的模式给予的(不是全部),并且需要额外的配置。然而,个人我的建议是:不。分布式事务是棘手足够的东西像SQL本地局域网上 - 添加Web服务到组合,通常被麻烦问(我认为)

Increased support for this is given by things like WCF in certain modes (not all), and requires additional configuration. However, personally my advice is: DON'T. Distributed transactions are tricky enough for things like SQL on a local LAN - add web services into the mix and it is usually asking for trouble (is my opinion).

不过,有些人相处,这些就好了 - 因人而异

However, some people get along with these just fine - YMMV.

这是你的错误:

 catch(Exception ex)
 {
     string s = ex.Message;
 }

您code是可能的想告诉你什么是错,但你去了,吞下了异常。

Your code is probably trying to tell you what is wrong, but you went and swallowed the exception.

我的猜测是,在分布式事务(可能是WCF调用)另一个元素失败,惜败整个事务。例外的会告诉你这

My guess is that another element in the distributed transaction (maybe a WCF call) failed, dooming the entire transaction. The exception will tell you this.

IMO,删除块的完全的。如果不好的事情发生,不要隐藏它们,然后不知道发生了什么事。让IT泡沫到顶层,在那里您现有的(咳嗽)中央的错误处理会的当然的写错误到错误日志中您经常检查。

IMO, remove the catch block completely. If bad things happen, don't hide them and then wonder what happened. Let it bubble up to the top level, where your existing (cough) central error handling will of course write that error into the error log that you check frequently.

这篇关于为什么交易不会回滚Web服务和数据库之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆