Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跨多个方法的TransactionScope不是ACID?_C#_Transactions_Transactionscope - Fatal编程技术网

C# 跨多个方法的TransactionScope不是ACID?

C# 跨多个方法的TransactionScope不是ACID?,c#,transactions,transactionscope,C#,Transactions,Transactionscope,我有这样一个代码: using (TransactionScope transactionScope = new TransactionScope()) { SetDefaults(products); // Map the SizeCollectionIds of the products _dataAccess.MapProductSizes(products); // Mas

我有这样一个代码:

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

我的意图是,如果在事务作用域中调用的方法中的任何地方发生错误,则事务将回滚,但经过一些测试后,情况似乎并非如此,我的数据将半生不熟。有什么我遗漏的吗?我是否必须将方法本身中的数据访问调用包装到它们自己的TransactionScope中才能使其工作?

看起来在DataAccess层中创建了多个数据库连接实例。尝试在DataAccess类构造函数中实例化数据库连接,并在DataAccess方法中使用它。您可能想阅读

您在数据访问方法中做了什么?你应该至少发布一个。如果您的数据访问方法正确,则上述操作应按您的预期进行。如果未调用1 ts.Complete,且2使用的连接已登记在ts中,则事务将回滚。请注意,已打开的连接不会自动登记在TransactionScope中。还要验证DA提供程序是否正确使用了TS,并且未完成的部分不是DA提供程序之外的副作用造成的。粗体部分非常重要。默认情况下,一些DAL(如LINQ2SQL)将在每次提交更改时打开新连接。其他提供程序可能会重用相同的连接。了解更多关于此处使用的提供程序的信息(可能是DAL的峰值)将得到更有用的答案。哦,那么我必须在创建TransactionScope后打开连接,对吗?@jjm340用于自动登记,是的:您可以手动登记。请参阅相应的so问题。否,我的数据访问层在其存在的整个过程中只打开一个连接,但根据上面的pst,似乎我需要在创建TransactionScope后创建该连接,否则该连接将不知道在Yes中登记什么事务;这是最合适的方法。检查此讨论http://stackoverflow.com/questions/934316/is-there-a-way-to-use-transactionscope-with-an-existing-connection