As far as I know, The MSDTC only gets involved when:
You're querying a view/table inside a transaction that is linked to another server.
You're using two SqlConnections (or whatever it is NHibernate uses) within a single TransactionScope
You're enlisting another transactional component (like MSMQ or the transactional file system) inside a TransactionScope.
Other situations not mentioned.
If I disable MSDTC and run the following code, I get (MSDTC on server 'Server' is unavailable) an error.
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();
}
}
}
Am I missing anything?
Update: The stored procedure contains a simple insert statement:
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: When shutting down the MSDTC, the exception is thrown when the execution reached to this line:
using (SqlDataReader rdr = SqlHelper.ExecuteReader(/*Tr*/Cnn, "spPurchaseOrderAdd", purchaseOrderInfo.ExpectedShipment.ShipmentID, purchaseOrderInfo.CreateDate, purchaseOrderInfo.CustomerNotes, purchaseOrderInfo.Status, purchaseOrderInfo.PurchaseOrderNumber))
which means the subsequent lines has no effect.
TransactionScope
, then the transaction gets promoted to the DTC. Otherwise it should stay local. – David Hoerster