带嵌套方法的TransactionScope - MySQL

时间:2013-07-18 08:24:03

标签: c# asp.net mysql transactions transactionscope

MYSQL是否支持TransactionScope而不将MySqlConnection对象作为参数传递? 或者它只适用于MS SQL 2008及更高版本?

例如:

public void Method()
{
    using (var scope = new System.Transactions.TransactionScope())
    {
        Delete();
        Insert();
        Update();

        scope.Complete();
    }
}

public void Update()
{
     using(MySqlConnection conn = new MySqlConnection())
     {
         // Update something in the Database
     }
}

public void Insert()
{
     using(MySqlConnection conn = new MySqlConnection())
     {
         // Insert something in the Database
     }
}

public void Delete()
{
     using(MySqlConnection conn = new MySqlConnection())
     {
         // Delete something from Database
     }
}

或者我应该使用MySqlConnection conn对象作为参数吗?

public void Method()
{
    using (var scope = new System.Transactions.TransactionScope())
    {
        using(MySqlConnection conn = new MySqlConnection())
        {
            Delete(conn);
            Insert(conn);
            Update(conn);
        }

        scope.Complete();
    }
}

public void Update(MysqlConnection conn)
{
    // Update something in the Database
}

public void Insert(MysqlConnection conn)
{
    // Insert something in the Database
}

public void Delete(MysqlConnection conn)
{
    // Delete something from Database
}

1 个答案:

答案 0 :(得分:0)

您无法在一个MySqlConnection中创建多个TransactionScope个对象:bug 50773

解决方案是升级到https://github.com/mysql-net/MySqlConnector does support TransactionScope。{/ p>