MySqlBulkLoader 与事务中的 MySqlCommand

MySqlBulkLoader with MySqlCommand In Transaction

我有一个 MySql 数据库,里面有九个表。我需要从 C# 中的异步作业动态上传到其中六个。但是,为了加快上传速度,其中四个使用MySqlBulkLoader,而另外两个使用手动上传方式。为了支持并发并防止数据库中的错误,我需要同时上传它们。为此,我决定使用事务来确保 "all or nothing" 类型的上传。

问题就在这里:MySqlCommand class 允许通过其 Transaction 属性 中的 MySqlTransaction class 进行交易支持, MySqlBulkLoader 允许原子上传(我使用 INNODB 作为引擎,参见 mysql - Can MySqlBulkLoader be used with a transaction?). However, the MySqlBulkLoader creates it's own transaction for atomic upload, while another separate transaction will be used for the MySqlCommand's I will be executing. This does not support serialization, and could still result in errors if one bulk loader completes it's job, yet the application closes before another can finish. This would be solvable if transactions could be nested; however, they cannot: php - Mysql transactions within transactions

我的问题如下:C# connector 中是否有任何方法允许 MySqlBulkLoaderMySqlTransaction 相关联,如果没有,是否有任何方法可以自动回滚之前 MySqlBulkLoader 所做的更改。我查看了 Connector.NET 6.9 提供的 API,似乎这可能通过 MySqlConnection.BeginTransaction() 方法实现,但来自 Werner Wolf 的问题表明并非如此。

编辑

我找到了 this question by Saravanan。但是,我认为这个问题与 MySqlBulkLoaderMySqlCommand.

混合的因素无关。

原题问:

is there any way in the C# connector to allow the MySqlBulkLoader to be associated with a MySqlTransaction
为此,似乎没有。仅仅将两个事务放在一起也不起作用,因为它不支持序列化(我认为它不会)。但是,另一个问题是:
if there is any way to automatically rollback changes made by previous MySqlBulkLoaders.
而且,似乎没有一种简单的方法来回滚更改。当然,我可以检查前后的tables状态,删除之前不存在的条目;但是,这个解决方案并不是最优的。但是,解决方案可能会以问题的形式出现。原始问题的答案假定无法将单独的交易与 MySqlBulkLoader 相关联;但没有指定我们可以自己制作一个(a.k.a,扩展 MySqlBulkLoader class 以包含 Transaction 实例参数)。这似乎是问题的解决方案,也是Werner Wolf提出的问题的解决方案。

然而,这根本不是解决方案。由于 MySqlBulkLoader 是 SQL 的 LOAD DATA INFILE 语法的简单包装器,它的行为与 SQL 查询一样。从 MySQL Reference Manual 中,我们得到了答案:

The statements listed in this section (and any synonyms for them) implicitly end any transaction active in the current session, as if you had done a COMMIT before executing the statement.
网页下方的声明是 LOAD DATA INFILE。这是我们问题的真实答案:不可能将 MySqlBulkLoaderMySqlTransaction 关联起来,至少不可能按照我们预期的方式工作。然而,如上所述,解决该问题的方法可能是检查每个 table 的主键的索引,锁定 table,保存它们的位置,然后手动执行 "rollback"上传失败后。