据我所知,MSDTC 仅在以下情况下参与:
您正在查询链接到另一台服务器的事务中的视图/表。
您在单个 TransactionScope 中使用两个 SqlConnections(或 NHibernate 使用的任何东西)
您正在 TransactionScope 中征用另一个事务组件(如 MSMQ 或事务文件系统)。
其他情况未提及。
如果我禁用 MSDTC 并运行以下代码,我会收到(服务器“服务器”上的 MSDTC 不可用)错误。
public bool Add(PurchaseOrderInfo purchaseOrderInfo)
{
using (TransactionScope ts = new TransactionScope())
{
using (SqlConnection Cnn = new SqlConnection(SqlHelper.ConnStr))
{
Cnn.Open();
try
{
using (SqlDataReader rdr = SqlHelper.ExecuteReader(/*Tr*/Cnn, "spPurchaseOrderAdd", purchaseOrderInfo.ExpectedShipment.ShipmentID, purchaseOrderInfo.CreateDate, purchaseOrderInfo.CustomerNotes, purchaseOrderInfo.Status, purchaseOrderInfo.PurchaseOrderNumber))
{
if (rdr.Read())
FillPurchaseOrderInfo(rdr, ref purchaseOrderInfo, GettingDepthEnum.Level_0);
else
{
return false;
}
}
foreach (PurchaseOrderDetailInfo detailInfo in purchaseOrderInfo.Details)
{
throw new Exception("Test");
//if (!AddPurchaseOrderDetail(Tr, purchaseOrderInfo, detailInfo))
{
//Tr.Rollback();
return false;
}
}
return true;
}
catch (Exception ex)
{
throw ex;
}
ts.Complete();
}
}
}
我错过了什么吗?
更新:存储过程包含一个简单的插入语句:
INSERT INTO tblPurchaseOrder
(ShipmentID, CreateDate, CustomerNotes, PurchaseOrderState, PurchaseOrderNumber, LastActivityDate)
VALUES
(@ShipmentID, @CreateDate, @CustomerNotes, @PurchaseOrderState, @PurchaseOrderNumber, GETDATE());
--Step 2: return row that INSERTED to Client Computer.
SELECT dbo.viwGetPurchaseOrderWeight.Weight,* FROM tblPurchaseOrder LEFT OUTER JOIN viwGetPurchaseOrderWeight ON viwGetPurchaseOrderWeight.PurchaseOrderID = tblPurchaseOrder.PurchaseOrderID WHERE (tblPurchaseOrder.PurchaseOrderID = Scope_Identity());
UPDATE2: 关闭 MSDTC 时,执行到此行时抛出异常:
using (SqlDataReader rdr = SqlHelper.ExecuteReader(/*Tr*/Cnn, "spPurchaseOrderAdd", purchaseOrderInfo.ExpectedShipment.ShipmentID, purchaseOrderInfo.CreateDate, purchaseOrderInfo.CustomerNotes, purchaseOrderInfo.Status, purchaseOrderInfo.PurchaseOrderNumber))
这意味着后续行无效。