0
現在正在使用WCF Restful Service。我需要維護服務調用中的事務,如果發生錯誤意味着必須回滾。所以我已經嘗試使用system.transactions的交易技術。休息服務調用的顯式事務處理
我可以從下面的C#代碼維護事務:
CommittableTransaction tx = new CommittableTransaction();
SqlConnection myConnection = new SqlConnection(@"Data Source=199.999.9.9;Initial Catalog=AWCFTest;Persist Security Info=True;User ID=sa;Password=sa");
SqlCommand myCommand = new SqlCommand();
myConnection.Open();
myConnection.EnlistTransaction(tx);
try
{
myCommand.Connection = myConnection;
myCommand.CommandText = "insert into Customer(CustomerName,CustomerCode) values('1','sss')";
myCommand.ExecuteNonQuery();
// Insert the first record.
myCommand.CommandText = "insert into Customer(CustomerName,CustomerCode) values('3','bbb')";
myCommand.ExecuteNonQuery();
tx.Commit();
catch (Exception ex)
{
tx.Rollback();
}
myConnection.Close();
tx = null;
}
,但不能從下面的代碼維護事務:
CommittableTransaction tx = new CommittableTransaction();
SqlConnection myConnection = new SqlConnection(@"Data Source=199.999.9.9;Initial Catalog=AWCFTest;Persist Security Info=True;User ID=sa;Password=sa");
SqlCommand myCommand = new SqlCommand();
myConnection.Open();
myConnection.EnlistTransaction(tx);
try
{
myCommand.Connection = myConnection;
string requestData = "{\"logInsert\":\"first\"}";
byte[] data = Encoding.UTF8.GetBytes(requestData);
HttpWebRequest request;
request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/RestfulServices/Common.svc/convert1");
request.Method = "PUT";
request.ContentType = "application/json";
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
String ss = new StreamReader(resp.GetResponseStream()).ReadToEnd();
string requestData1 = "{\"logInsert\":\"second\"}";
byte[] data1 = Encoding.UTF8.GetBytes(requestData1);
HttpWebRequest request1;
request1 = (HttpWebRequest)HttpWebRequest.Create("http://localhost/RestfulServices/Common.svc/convert2");
request1.Method = "PUT";
request1.ContentType = "application/json";
Stream dataStream1 = request1.GetRequestStream();
dataStream1.Write(data1, 0, data1.Length);
dataStream1.Close();
HttpWebResponse resp1 = (HttpWebResponse)request1.GetResponse();
String ss1 = new StreamReader(resp1.GetResponseStream()).ReadToEnd();
tx.Commit();
}
catch (Exception ex)
{
tx.Rollback();
}
myConnection.Close();
tx = null;
}
是否有維護事務的任何可能的方式休息服務電話?還要回滾?
對不起同樣的問題...數據插入數據庫不回滾! –