如何验证 TransactionScope 是否适用于 MySQL?

How to verify TransactionScope works with MySQL?

我想添加读未提交隔离 level.i 知道可以使用 SQL 语句。但我想尝试使用 TransactionScope,我尝试了但没有在 MySQL 常规日志上获得任何隔离语句。 问题 1 transactionscope class 是否与 MySQL 一起工作。 如果是那么如何验证。

I want to add read uncommitted isolation level.i know it is possible to do with SQL statement.

"standard" 方法是使用 MySqlConnection.BeginTransaction API:

using (var transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted))
{
    // do stuff
    transaction.Commit();
}

does transactionscope class work with MySQL

是的,MySQL Connector/NET 可以支持 TransactionScope,但不支持分布式事务。 known issue Connector/NET 不支持 TransactionScope 的 XA 事务。

如果要使用TransactionScope,那么MySQL Connector/NET应该继承它的隔离级别:

var txOptions = new System.Transactions.TransactionOptions();
txOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;

using(var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions))
using (var connection = new MySqlConnection("... connection string ..."))
{
    connection.Open();
    // ...
    transaction.Complete();
}

如果你需要用TransactionScope支持真正的分布式事务,那么切换到MySqlConnector as your ADO.NET driver. Unlike Connector/NET, it fully supports distributed transactions