Transaction aborted in Using TransactionScope
In our application, during save operation we are data to almost 15 child tables. When some error occurred, we have data discrepancy in tables. So we decided to use TransactionScope. When we use that it gives error saying "Transaction has aborted", StackTrace display following.
at System.Transactions.TransactionStateAborted.BeginCommit(InternalTransaction tx, Boolean asyncCommit, AsyncCallback asyncCallback, Object asyncState)
at System.Transactions.CommittableTransaction.Commit()
at System.Transactions.TransactionScope.InternalDispose()
at System.Transactions.TransactionScope.Dispose()
开发者_开发技巧Our code looks like
using (TransactionScope tScope = new TransactionScope())
{
dataManager.AddFood(food);
if (!ModelState.IsValid)
throw new ApplicationException("Model state is invalid");
if (beingCloned)
food.IsUserFood = false;
dataManager.Save();
if (!food.IsUserFood)
dataManager.GenerateSearchRecords(food.FoodID);
FoodManager foodManager = new FoodManager();
foodManager.CopyFoodToHistory(food);
if (beingCloned)
{
foodManager.CreateUserFoodCopyForClonedFood(food);
HttpPostedFileBase file = ControllerContext.HttpContext.Request.Files["Image"];
if (file != null && file.FileName.Length > 0)
foodManager.UpdateMediaCopyForClonedFood(food, true);
else
foodManager.UpdateMediaCopyForClonedFood(food);
}
tScope.Complete();
}
Thanks in advance
TransactionScope can be used with multiple DataContexts, but as soon as more than one connection is involved the transaction is escalated to a MSDTC/XA/distributed transaction. For that to work you need to have MSDTC running on both the system where you code runs and on the database server.
Alternatively, you can avoid escalation to a distributed transaction if you create an explicit connection within the transactionscope and pass that to your datacontexts; that way the TransactionScope will not escalate to a distributed transaction, and won't rely on MSDTC...
https://stackoverflow.com/a/1592133/102937
精彩评论