跨多种方法的TransactionScope不是ACID?

时间:2012-06-21 00:46:36

标签: c# transactions transactionscope

我有这样的代码:

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中将方法本身中的数据访问调用包装起来才能使其生效?

1 个答案:

答案 0 :(得分:0)

在DataAccess层中看起来像是创建了几个数据库连接实例。尝试在DataAccess类构造函数中实例化数据库连接,并在DataAccess方法中使用它。您可能需要阅读this blog post