简体   繁体   English

跨多种方法的TransactionScope不是ACID?

[英]TransactionScope across Multiple Methods is not ACID?

I have a code like this: 我有这样的代码:

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();
        }

What I intend, is that if an error occurs anywhere in the methods called in the transaction scope, that the transaction is rolled back, but after some testing it seems that this is not the case and my data ends up half baked. 我想要的是,如果在事务范围内调用的方法中的任何地方发生错误,那么事务就会被回滚,但是经过一些测试后,似乎情况并非如此,我的数据最终会被烧掉一半。 Is there something I'm missing? 有什么我想念的吗? Do I have to wrap the data access calls within the methods themselves in their own TransactionScopes to get this to work? 我是否必须在自己的TransactionScopes中将方法本身中的数据访问调用包装起来才能使其工作?

Looks like in your DataAccess layer several instances of the database connections are created. 在您的DataAccess层中看起来像是创建了几个数据库连接实例。 Try to instantiate your database connection in the DataAccess class constructor and use it across your DataAccess methods. 尝试在DataAccess类构造函数中实例化数据库连接,并在DataAccess方法中使用它。 You may want to read this blog post 您可能想阅读此博文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM