[英]TransactionScope across Multiple Methods is not ACID?
我有这样的代码:
using (TransactionScope transactionScope = new TransactionScope())
{
SetDefaults(products);
// Map the SizeCollectionIds of the products
_dataAccess.MapProductSizes(products);
// Mass update and insert missing parent records to the database
_dataAccess.UpdateParents(products);
// Get ids of parent products that were newly inserted
_dataAccess.PopulateParentProductByParentSku(products);
// Insert children into database
_dataAccess.InsertProducts(products);
// Insert the UPCs into the database
_dataAccess.InsertUPCs(products);
// Get Product Ids of newly inserted records
_dataAccess.PopulateProductIds(products);
// Get just the parent products to insert the brands
List<ParentProduct> parents = (from prod in products
select prod.ParentProduct).Distinct().ToList();
// Insert ParentProductBrand record
_dataAccess.InsertParentProductBrands(parents);
// Insert the custom attribute records
_dataAccess.InsertProductCustomAttributes(products);
transactionScope.Complete();
}
我想要的是,如果在事务范围内调用的方法中的任何地方发生错误,那么事务就会被回滚,但是经过一些测试后,似乎情况并非如此,我的数据最终会被烧掉一半。 有什么我想念的吗? 我是否必须在自己的TransactionScopes中将方法本身中的数据访问调用包装起来才能使其工作?
在您的DataAccess层中看起来像是创建了几个数据库连接实例。 尝试在DataAccess类构造函数中实例化数据库连接,并在DataAccess方法中使用它。 您可能想阅读此博文
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.