Why transaction not be rolled back between a web service and database?
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.
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.WpSv开发者_开发问答Password,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;
}
Refer to this question please:Transaction
Edit:
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.
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.
Here's your error:
catch(Exception ex)
{
string s = ex.Message;
}
Your code is probably trying to tell you what is wrong, but you went and swallowed the exception.
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, 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.
精彩评论