Devart Connectory Mysql使用事务范围的正确方法

本文关键字:方法 范围 事务 Connectory Mysql Devart | 更新日期: 2023-09-27 18:24:14

我们一直在使用带有.net连接器的C#和mysql,但使用单独提交,有时无法提交所有内容。所以我们现在转向这个工具http://www.devart.com/dotconnect/mysql/支持分布式事务。一切都很好,只是我们不太确定如何放置多重连接。方法1是将每个连接嵌套到另一个连接中。方法2是单独的。我们应该关闭连接或由transScope.Complete()处理的位置;和transScope.Dispose();

方法1。

using (TransactionScope transScope = new TransactionScope())
{
     string myConnStringLocal = "User Id=***;Password=****;Host=" + globalSettings.settingLocalIP + ";Database=" + globalSettings.settingLocalDB;
     using (MySqlConnection connectionLocal = new MySqlConnection(myConnStringLocal))
     {
       connectionLocal.open() 
       string myConnStringCentral = "User Id=***;Password=*****;Host=" + globalSettings.settingCentralIP + ";Database=" + globalSettings.settingCentralDB;
         using (MySqlConnection connectionCentral = new MySqlConnection(myConnStringCentral))
         {
             connectionCentral.Open();
               string myConnStringCentralCopy = "User Id=*****;Password=*****;Host=" + globalSettings.settingCentralCopyIP + ";Database=" + globalSettings.settingCentralCopyDB;
              using (MySqlConnection connectionCentralCopy = new MySqlConnection(myConnStringCentralCopy))
             {
                 connectionCentralCopy.Open();   
             }
         }
     }
     if (rollbackBoolean == 0)
        transScope.Complete();
     else
        transScope.Dispose();
}

方法2

using (TransactionScope transScope = new TransactionScope())
{
     string myConnStringLocal = "User Id=***;Password=****;Host=" + globalSettings.settingLocalIP + ";Database=" + globalSettings.settingLocalDB;
     using (MySqlConnection connectionLocal = new MySqlConnection(myConnStringLocal))
     {
         connectionLocal.open() 
     }
     string myConnStringCentral = "User Id=***;Password=*****;Host=" + globalSettings.settingCentralIP + ";Database=" + globalSettings.settingCentralDB;
    using (MySqlConnection connectionCentral = new MySqlConnection(myConnStringCentral))
    {
       connectionCentral.Open();
    }
    string myConnStringCentralCopy = "User Id=*****;Password=*****;Host=" + globalSettings.settingCentralCopyIP + ";Database=" + globalSettings.settingCentralCopyDB;
    using (MySqlConnection connectionCentralCopy = new MySqlConnection(myConnStringCentralCopy))
    {
        connectionCentralCopy.Open();   
    }
    if (rollbackBoolean == 0)
        transScope.Complete();
    else
        transScope.Dispose();
}

Devart Connectory Mysql使用事务范围的正确方法

这两种方法都是正确的。在这两种情况下,TransactionScope都将与dotConnect for MySQL一起使用。不需要以下代码:

else
transScope.Dispose();

因为Dispose方法在退出CCD_ 1块时被自动调用。

。。。我经历了它,这意味着在我的情况下,当我调用完整的仪式时,transactionScope将关闭它

否,transScope.Complete()不会关闭连接。如果连接在TransactionScope的using块内关闭,则连接对象将关闭,但是,如果调用transScope.Dispose()之前是transScope.Complete(),则内部连接将保持打开状态,以便在调用transScole.Dispose时向数据库发送更改。如果为连接对象调用了Close或Dispose方法,transScope.Dispose()将关闭内部连接。如果连接对象未关闭,transScope.Dispose()将不对连接执行任何操作。

。。。在我的每个连接中,我都会跟踪try-and-catch,如果有任何错误,我会将rollbackBoolean标记为1,这样它就不会完成,整个事务应该回滚,这是一个正确的机制吗

如果发生错误,请不要调用Complete。如果未调用Complete方法,则在执行Dispose方法时将回滚事务。

下面是一个带有try/catch块并在出现错误时回滚事务的示例:

using (TransactionScope transScope = new TransactionScope())
{
   try
   {
      string myConnStringLocal = "User Id=***;Password=****;Host=" + globalSettings.settingLocalIP + ";Database=" + globalSettings.settingLocalDB;
      using (MySqlConnection connectionLocal = new MySqlConnection(myConnStringLocal))
      {
         connectionLocal.Open();
      }
      string myConnStringCentral = "User Id=***;Password=*****;Host=" + globalSettings.settingCentralIP + ";Database=" + globalSettings.settingCentralDB;
      using (MySqlConnection connectionCentral = new MySqlConnection(myConnStringCentral))
      {
         connectionCentral.Open();
      }
      string myConnStringCentralCopy = "User Id=*****;Password=*****;Host=" + globalSettings.settingCentralCopyIP + ";Database=" + globalSettings.settingCentralCopyDB;
      using (MySqlConnection connectionCentralCopy = new MySqlConnection(myConnStringCentralCopy))
      {
         connectionCentralCopy.Open();
      }
      transScope.Complete();
      Console.WriteLine("Transaction is completed");
   }
   catch (Exception)
   {
      Console.WriteLine("Transaction is rolled back");
   }
}