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

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

我的意图是,如果在事务范围内调用的方法中的任何地方发生错误,事务就会回滚,但经过一些测试后,情况似乎并非如此,我的数据最终半生不熟。有什么我想念的吗?我是否必须将数据访问调用包装在方法本身的 TransactionScope 中才能使其正常工作?

最佳答案

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

关于c# - 跨多个方法的 TransactionScope 不是 ACID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11130155/

相关文章:

ruby-on-rails - 如何在 ActiveRecord 事务中获得保存(无感叹号)语义?

c# - 将 TransactionScope 与 System.Data.OracleClient 结合使用 - TransactionAbortedException

sql-server - TransactionScope 和连接池

c# - 如何在 C# 中的自定义对象上使用 foreach 关键字

c# - 如何将对象参数传递给 WCF 服务?

mysql - Innodb交易和房间预订

php - 自动创建 MySQL 表 [用户控制]

c# - 在集成测试 Web API 时使用 TransactionScope

C# 等效于 Python repr()

c# - 由于缺少许可证,无法构建 Brew 包